0
votes

I would like to utilize Xrm.Page.ui.setFormNotification to display a banner at the top of a Shipment record. This banner would only appear for Shipments where the related entity Account is classified as "Service Watch".

I'm pretty new to Javascript so I'm a bit lost how to pull values from related entities of a record to use.

Xrm.Page.ui.setFormNotification("This Account is currently under Service Watch", "WARNING")

EDIT: Code working;

    function checkServiceWatch() {
    try{
        var account = Xrm.Page.getAttribute("cmm_account").getValue();
        var accountid = account[0].id;
        var formattedGuid = accountid.replace("}", "");
        accountid = formattedGuid.replace("{", "");
         "/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);
    }   
}
1

1 Answers

0
votes

You have to query the Account lookup record on form load to pull the extra attribute which says "Service watch" and show the notification banner if so.

You can refer this community thread & use the sample code as is or Xrm.Webapi method to do it based on your CRM version.

function checkAccount(){

    var accountid = Xrm.Page.data.entity.attributes.get("new_parentaccount").getValue()[0].id;

    if (accountid.startsWith("{") && accountid.endsWith("}"))
    {
    accountid = accountid.slice(1, accountid.length - 1);
    }


    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts(" + accountid + ")?$select=new_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["new_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(this.statusText);
    }
    }
    };
    req.send();

}