javascript - 如何使用JS查询父记录
问题描述
检查 paymenttype 是否等于 credit account [实体形式:auto_paymenttype]
那么,如果payment amount <= resit amount,则保存 else > prevent save (弹窗无效:payment amount 应该低于resit amount)[entity form: auto_resittype]
嗨,伙计们,如果有人可以重新编码并帮助我,那就太好了。我是 D365 和 JS 的新手。基本上,我有 auto_paymenttype 和 auto_resittype 实体,它们的父级是 Payment。如何使用JS查询父调整记录。我已经提供了我当前的代码,请帮我检查一下。我已经尝试了一切,但到目前为止还没有运气。对不起我不专业的照片。但我希望你能理解并帮助我为这种情况编写代码。谢谢你。
function resitApproveAmount(executionContext) {
try {
const object = {};
object.fctx = executionContext.getFormContext();
object.saveEvent = object.fctx.getEventArgs();
object.paymentamount = object.fctx.getAttribute("auto_paymentamount").getValue();
object.resitamount = object.fctx.getAttribute("auto_resitamount").getValue();
object.paymenttype = Xrm.Page.getAttribute("auto_paymenttype").getValue();
if (object.paymenttype != null) {
object.autoGUID = object.paymenttype[0].id.substring(1, 37);
}
Xrm.WebApi.retrieveMultipleRecords("auto_paymenttype", "$select=auto_name").then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
object.auto_name = result.entities[i]["auto_name"];}
if(object.auto_name == "Credit Account"){
if (object.paymenttamount >= object.resitamount) {
alert("Payment Amount cannot be more than Resit Amount.");
object.saveEvent.preventDefault();
}
else
{object.fctx.data.save();}
}
},
function (error) {
console.log(error.message);
}
);
}
catch (error) {
console.log(error);
}
}
解决方案
您的代码暗示auto_paymentamount
并存auto_resitamount
在于 entity 上,Payment
但您 ERD 显示它们存在于 entity 上Resit Type
。
如果他们在Payment
实体上,那么您可以在您的问题中检索他们的值:
const paymentAmount = formContext.getAttribute("auto_paymentamount").getValue();
const resitAmount = formContext.getAttribute("auto_resitamount").getValue();
否则,您需要使用 WebApi检索Resit Type
与您的记录相关的记录。Payment
如果是这种情况,我假设您从Payment
to查找Resit Type
。
假设 auto_paymentamount
并存auto_resitamount
在于实体上,Payment
您的代码可以简化为以下两种解决方案。onsave
两种解决方案都假定在事件期间调用此函数。
解决方案 1 - 检索相关的付款类型记录
function resitApproveAmount(executionContext) {
try {
// Get the form context
const formContext = executionContext.getFormContext();
// Extract attribute values from the form
const paymentAmount = formContext.getAttribute("auto_paymentamount").getValue();
const resitAmount = formContext.getAttribute("auto_resitamount").getValue();
const paymentTypeLookup = formContext.getAttribute("auto_paymenttype").getValue();
// Exit as payment type is not set
if (!paymentTypeLookup) return;
// Extract the payment type record ID from the payment type lookup
const paymentTypeId = paymentTypeLookup[0].id.substring(1, 37);
// Retrieve a SINGLE auto_paymenttype based on lookup ID on form
Xrm.WebApi.retrieveRecord("auto_paymenttype", paymentTypeId, "$select=auto_name").then(
function (paymentType)
{
// If the payment type is credit account then check payment amount and resit amount
if (paymentType.auto_name.toLowerCase() == "Credit Account".toLowerCase())
{
if (paymentAmount >= resitAmount) {
formContext.getEventArgs().preventDefault();
Xrm.Navigation.openErrorDialog({message:"Payment Amount cannot be more than Resit Amount."})
}
}
//Otherwise do nothing
},
function (error)
{
console.log(error.message);
}
);
}
catch (error)
{
console.log(error);
}
}
解决方案 2 - 硬编码付款类型记录 ID
如果您可以保证您的支付类型记录 ID 在所有环境中都相同(即使用数据迁移工具迁移支付类型记录),则可以使用此方法
function resitApproveAmount(executionContext) {
try {
// Get the form context
const formContext = executionContext.getFormContext();
// Extract attribute values from the form
const paymentAmount = formContext.getAttribute("auto_paymentamount").getValue();
const resitAmount = formContext.getAttribute("auto_resitamount").getValue();
const paymentTypeLookup = formContext.getAttribute("auto_paymenttype").getValue();
// Exit as payment type is not set
if (!paymentTypeLookup) return;
// Extract the payment type record ID from the payment type lookup
const paymentTypeId = paymentTypeLookup[0].id.substring(1, 37);
// Exit if payment type id is not "Credit Account"
if (paymentTypeId.toLowerCase() !== "Hard Code Credit Account GUID here / retrieve environment variable") return;
if (paymentAmount >= resitAmount) {
formContext.getEventArgs().preventDefault();
Xrm.Navigation.openErrorDialog({message:"Payment Amount cannot be more than Resit Amount."})
}
}
catch (error)
{
console.log(error);
}
}
笔记
为了帮助将来解决此类问题,我建议您绘制实体关系图。这有助于我们了解您的实体模型和关系,这有助于我们在第一时间准确回答问题。
Diagrams.net是免费绘制 ERD 的好方法
推荐阅读
- ghostscript - Ghostscript -d vs -s 命令行参数
- sql - 加入表级数据条件
- python - 有没有办法知道一个方法是否是一个运算符?
- sql - 尝试从电子应用程序连接到 sql server 但未找到端口错误
- email - 两个字段的 Mutt 别名
- typescript - React Native/Typescript - 错误模块 '"react-native"' 没有导出的成员 'Pressable'.ts(2305)
- r - 如何按R中的特定行名划分列中的所有行
- data-modeling - 数据保险库建模
- octobercms - 在前端使用后端表单 - OctoberCMS
- kentico - 如何强制 Live Url (AbsoluteUrl) 在 Kentico 页面类型上更新