如何忽略 CRM 查找字段中的 NULL 值?

我有以下代码设置 onLoad 以在关联帐户标记为“服务监视”时在“发货”记录上生成横幅。该代码当前可以运行,但它会生成错误警报"unable to get property '0' of undefined or null reference"。当用户创建新的货件记录时会发生此错误,因为帐户字段尚无值。


如何配置代码以忽略帐户字段中的 NULL 值?


function checkServiceWatch() {

    try{

        var account = Xrm.Page.getAttribute("cmm_account").getValue();

        var accountid = account[0].id;

        var formattedGuid = accountid.replace("}", "");

        accountid = formattedGuid.replace("{", "");

        // alert("Accountid: " + accountid);  // does that ID have brackets around it?

        // alert("Request: " + Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountid + ")?$select=cmm_servicewatch");


        var req = new XMLHttpRequest();

        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountid + ")?$select=cmm_servicewatch", true);

        req.setRequestHeader("OData-MaxVersion", "4.0");

        req.setRequestHeader("OData-Version", "4.0");

        req.setRequestHeader("Accept", "application/json");

        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        req.onreadystatechange = function()



应忽略正在创建的记录,但会在具有帐户值的现有货件上生成横幅。


慕斯709654
浏览 177回答 3
3回答

汪汪一只猫

您必须检查帐户变量是否已正确初始化。如果有,返回该变量将等同于true,如果它不存在,它将返回false并且不运行该部分中的其余代码try。正确的代码如下:function checkServiceWatch() {    try{        var account = Xrm.Page.getAttribute("cmm_account").getValue();        if(account) {        var accountid = account[0].id;        var formattedGuid = accountid.replace("}", "");        accountid = formattedGuid.replace("{", "");        // alert("Accountid: " + accountid);  // does that ID have brackets around it?        // alert("Request: " + Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountid + ")?$select=cmm_servicewatch");        var req = new XMLHttpRequest();        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountid + ")?$select=cmm_servicewatch", true);        req.setRequestHeader("OData-MaxVersion", "4.0");        req.setRequestHeader("OData-Version", "4.0");        req.setRequestHeader("Accept", "application/json");        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");        req.onreadystatechange = function()        {            if (this.readyState === 4)             {                req.onreadystatechange = null;                if (this.status === 200)                 {                    var result = JSON.parse(this.response);                    var serviceWatch = result["cmm_servicewatch"];                    // alert("serviceWatch: " + serviceWatch);                    if(serviceWatch) //set notification                    {                        Xrm.Page.ui.setFormNotification("This Account is currently under Service Watch","WARNING","1");                         } // else                     // {                    //   //Xrm.Page.ui.clearFormNotification("1");                    // }                  }                 else                 {                    Xrm.Utility.alertDialog("Status: " + this.status + ", Text: " + this.statusText);                }            }        };        req.send();    }    }    catch (err) {        alert("ServiceWatchCheckRibbon | checkServiceWatch " + err.message);    }   }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript