You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Javascript查询D365中adjustment invoice的父级case_adjustment记录

D365 JS查询父级关联记录问题修复方案

故障排查点

你提到代码无法执行adjustmentTypeLookup相关逻辑,可优先排查以下问题:

  • 字段逻辑名错误:进入D365表单编辑器,双击你用到的case_adjustmenttype查找字段,确认显示的逻辑名和代码里的拼写完全一致,注意自定义前缀是否正确
  • 事件绑定时机错误:如果代码绑定的是表单加载(onLoad)事件,新建adjustment invoice记录时case_adjustmenttype还未赋值,会直接触发if (!adjustmentTypeLookup) return逻辑直接退出;建议绑定到case_adjustmenttype字段的onChange事件,或者表单保存事件
  • 业务逻辑不匹配:你的需求是查询父级case_adjustment记录,但现有代码查询的是调整类型case_adjustmenttype实体,如需实现查询父级记录的需求,需要将读取的lookup字段替换为你关联父级case_adjustment的字段。

修正后代码示例

function adjustmentInvoiceApproveAmount(executionContext) {
    try {
        const formContext = executionContext.getFormContext();
        // 读取字段值
        const adjustmentAmount = formContext.getAttribute("case_adjustmentamount").getValue();
        const amountDue = formContext.getAttribute("case_amountdue").getValue();
        // 若要查询父级case_adjustment,此处替换为表单上关联父级的lookup字段逻辑名即可
        const adjustmentTypeLookup = formContext.getAttribute("case_adjustmenttype").getValue();

        if (!adjustmentTypeLookup) {
            console.log("调整类型字段未赋值,逻辑退出");
            return;
        }

        // 兼容新旧版API的GUID格式,自动去除括号
        const adjustmentTypeId = adjustmentTypeLookup[0].id.replace(/[{}]/g, '');
         
        Xrm.WebApi.retrieveRecord("case_adjustmenttype", adjustmentTypeId, "$select=case_name").then(
            function success(adjustmentType) {
                if (adjustmentType?.case_name?.toLowerCase() === "credit notes") {
                    if (adjustmentAmount >= amountDue) {
                        formContext.getEventArgs().preventDefault();
                        Xrm.Navigation.openErrorDialog({message:"付款金额不能超过补缴金额"});
                    }
                }
            },
            function (error) {
                console.log("查询失败:" + error.message);
            }
        );
    } 
    catch (error) {
        console.log("代码执行异常:" + error.message);
    }
}

调试建议

可以在const adjustmentTypeLookup = ...代码后加一行console.log(adjustmentTypeLookup),打开浏览器控制台查看字段值的返回情况,就能快速定位是字段取值失败还是后续逻辑问题。

内容的提问来源于stack exchange,提问作者Farhan Asyraaf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 00:27:03