【问题标题】:How Can I Query the Parent Record Using JS如何使用JS查询父记录
【发布时间】:2021-08-30 11:31:02
【问题描述】:

检查paymenttype是否等于credit account[实体形式:auto_paymenttype]

然后,如果支付金额 阻止保存(弹窗无效:支付金额应低于重新支付金额)[实体形式: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);
        }
    }

click here for picture overview

【问题讨论】:

    标签: javascript dynamics-crm office365api


    【解决方案1】:

    您的代码暗示 auto_paymentamountauto_resitamount 存在于实体 Payment 上,但您的 ERD 显示它们存在于实体 Resit Type 上。

    如果它们在 Payment 实体上,那么您可以在问题中检索它们的值:

    const paymentAmount = formContext.getAttribute("auto_paymentamount").getValue();
    const resitAmount = formContext.getAttribute("auto_resitamount").getValue();
    

    否则,您需要使用 WebApi 检索与您的 Payment 记录相关的 Resit Type 记录。如果是这种情况,我假设您从 Payment 查找到 Resit Type

    假设 auto_paymentamountauto_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 的好方法

    【讨论】:

      【解决方案2】:

      我再次阅读了这个问题,并认为我更好地理解了您的实体模型:

      与我之前的回答一样,此解决方案假定在 Payment 的 onsave 事件期间调用此函数。我使用 async/await 和 async onsave events 来简化对 Payment TypeResit Type 的检索。像我的其他答案一样,如果您知道付款类型 GUID 在不同环境中是相同的,您可以硬编码付款类型 ID,并在付款时根据查找 ID 检查 ID,以节省 WebApi 调用.

      /**
       * On Save of Payment record
       * @param executionContext 
       * @returns 
       */
      
      async function onSave(executionContext) {
          try {
              // Get the form context
              const formContext = executionContext.getFormContext();
              const paymentTypeLookup = formContext.getAttribute("auto_paymenttype").getValue();
              const resitTypeLookup = formContext.getAttribute("aut_resittype").getValue();
      
              // Exit as payment type is not set
              if (!paymentTypeLookup || !resitTypeLookup) return;
      
              // Extract related entity IDs
              const paymentTypeId = paymentTypeLookup[0].id.substring(1, 37);
              const resitTypeId = resitTypeLookup[0].id.substring(1, 37);
      
              // Get related records
              const [paymentTypeResult, resitTypeResult] = await Promise.all([
                  Xrm.WebApi.retrieveRecord("auto_paymenttype", paymentTypeId),
                  Xrm.WebApi.retrieveRecord("auto_resittype", resitTypeId)
              ]);
      
              // Evaluate payment type and resit type and prevent save if required
              if (paymentTypeResult.auto_name.toLowerCase() === "Credit Account".toLowerCase() 
              && resitTypeResult.auto_paymentAmount >= resitTypeResult.auto_resitAmount) 
              {
                  formContext.getEventArgs().preventDefault();
                  Xrm.Navigation.openErrorDialog({message:"Payment Amount cannot be more than Resit Amount."})
              }
          } 
          catch (error) 
          {
              console.log(error);
          }
      }
      

      【讨论】:

      • 非常感谢您的回答!我已经尝试了解决方案 1,但是我在运行它时遇到问题,从我打开调试器时开始,const resitTypeLookup = formContext.getAttribute("aut_resittype").getValue(); 直到检索记录 Api。所以这意味着它无法从 paymentTypeLookup 中获得价值,我不知道为什么。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2015-01-01
      • 2011-01-05
      相关资源
      最近更新 更多