0

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);
    }   
}
Pringle
  • 35
  • 8

1 Answers1

0

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

}
  • Arun thanks for the quick response, really appreciate it. When I try using your script, I get an undefined error prompt on form load. I'm assuming it's having an issue pulling the Account? – Pringle Sep 05 '19 at 13:15
  • @Pringle were you able to sort this out? – Arun Vinoth-Precog Tech - MVP Sep 16 '19 at 18:37
  • Arun thanks so much for the head start on this. I ended up going with the following (refer to original post above), and it works well. However, we're getting an error alert on Shipment record creation. This is due to Account field having no value initially, because the record is being created. How do I go about ignoring NULL values in the Account field? – Pringle Sep 17 '19 at 13:03
  • @Pringle can you add a new question & show the code where you're getting error? – Arun Vinoth-Precog Tech - MVP Sep 17 '19 at 13:36
  • Thanks Arun, I have done so here https://stackoverflow.com/questions/57993707/how-to-ignore-null-value – Pringle Sep 18 '19 at 13:17