首页 > 解决方案 > 如何使用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);
        }
    }

点击这里查看图片概览

标签: javascriptdynamics-crmoffice365api

解决方案


您的代码暗示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如果是这种情况,我假设您从Paymentto查找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 的好方法

在此处输入图像描述


推荐阅读