如何忽略CRM查找字段中的NULL值? [英] How to ignore NULL Value in CRM lookup field?

查看:95
本文介绍了如何忽略CRM查找字段中的NULL值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下代码设置为onLoad,以便每当关联的帐户被标记为服务监视时在发货记录上生成横幅。该代码当前有效,但是会生成错误警报无法获取未定义或空引用的属性 0 。当用户创建新的货运记录时,由于帐户字段尚无值,因此会发生此错误。

I have the following code setup onLoad to generate a banner on a 'Shipment' record whenever the associated Account is marked as "Service Watch". The code currently functions, however it's generating an error alert "unable to get property '0' of undefined or null reference". This error occurs when the user is creating a new Shipment record, as the Account field doesn't have a value yet.

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

How can I configure the code to ignore a NULL value in the Account field?

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);
    }   
}

应忽略正在创建的记录,但会在其上生成标语

Should ignore records being created, but generate a banner on existing Shipments with Account values.

推荐答案

您必须检查account变量是否已正确初始化。如果有,返回变量将等效于 true ,如果不存在,则将返回 false 并不要运行 try 部分中的其余代码。正确的代码如下:

You have to check that the account variable has been initialised correctly. If it has, returning the variable will be equivalent to true and if it doesn't exist it will return false and not run the rest of the code in the try section. Correct code is below:

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);
    }   
}

这篇关于如何忽略CRM查找字段中的NULL值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆