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

How to ignore NULL Value in CRM lookup field?

我在 onLoad 中设置了以下代码,以便在关联帐户标记为 "Service Watch" 时在 'Shipment' 记录上生成横幅。该代码目前可以正常运行,但它会生成错误警报 "unable to get property '0' of undefined or null reference"。当用户创建新的 Shipment 记录时会发生此错误,因为 Account 字段还没有值。

如何配置代码以忽略“帐户”字段中的 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()
        {
            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);
    }   
}

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

假设 account 将持有一个 array 并且 account id 不是 0

您可以使用 length 属性 来检查该帐户是否存在。如果是,那么继续,否则你可以 return

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

var accountid = account.length && account[0].id;

if(!accountid) return;

此行将 return id 放入 accountid if exist returns 0

var accountid = account.length && account[0].id;

如果您不确定帐户的第一个元素中是否存在 id (account[0]),您还可以添加额外的检查添加

var accountid = account.length && (account[0] || {}).id;

如果您的 account 变量中有元素但缺少键 id

仅当帐户存在时 accountid 变量将保存帐户 id,否则它将具有 0 或 undefined,无论您是否愿意继续,都可以相应地处理。

如果我解决了你的问题,请告诉我。

您必须检查帐户变量是否已正确初始化。如果有,returning 变量将等同于 true,如果它不存在,它将 return 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);
    }   
}

我会一直这样。只需使用它来进行空检查,停止执行 & return.

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

    if(account != null) 
        accountid = account[0].id.replace("}", "").replace("{", "");
    else
        return;

请注意,Xrm.Page 已弃用,将被更新的 ExecutionContext 取代。如需更多信息,请访问 https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/execution-context

我已将您代码的相关部分修改为新的 ExecutionContext,但如果您不想切换(还),您仍然可以使用 Xrm.Page 库。

function checkServiceWatch(executionContext) {
    var formContext = executionContext.getFormContext();
    var account = formContext.getAttribute("cmm_account").getValue();
    if (!account) {
        return;
    }
    var accountid = account[0].id;
    // removed further code for brevity.
}

if (!account) 语句检查帐户是否没有未定义或空值。布尔语句作为 nullundefined(以及 NaN0 和空字符串)将在布尔上下文中解析为 false。