Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JavaScript Banner

Posted on by 45

Hello everyone!

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.  Any help would be appreciated! 

*This post is locked for comments

  • Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: JavaScript Banner

    You are right. Here is the complete code after corrections. Please test and let me know the response

    function serviceWatchCheck() {

    //execute the query and get the results

    var accountId;

    if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) {

    accountId = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

    }

    Xrm.WebApi.retrieveRecord("account", accountId, "?$select=cmm_servicewatch").then(function (result) {

    //Show the warning message

    if (data["cmm_servicewatch"] != null && data["cmm_servicewatch"]=="Yes")

    {

    var message = “This Account Is Under Service Watch”;

    var type = “WARNING”; //INFO, WARNING, ERROR

    var id = “123”; //Notification Id

    var time = 5000; //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    }

    }, function (error) {

    //show error message if something error occured during operation

    var message = error.message;

    var type = “ERROR”; //INFO, WARNING, ERROR

    var id = “213”; //Notification Id

    var time = 5000; //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    });

    }

  • PeterJK Profile Picture
    PeterJK 45 on at
    RE: JavaScript Banner

    Like this?

    Like this?

    function serviceWatchCheck() {

    //execute the query and get the results

    var accountId;

    if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) {

    accountId = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

    }

    Xrm.WebApi.retrieveRecord("account", accountId, "?$select=cmm_servicewatch").then(function (result) {

    //Show the warning message

    if (data["cmm_servicewatch"] === null && data["cmm_servicewatch"]==="Yes")

    {

    var message = “This Account Is Under Service Watch”;

    var type = “WARNING”; //INFO, WARNING, ERROR

    var id = “123”; //Notification Id

    var time = 5000; //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    }

    }, function (error) {

    //show error message if something error occured during operation

    var message = error.message;

    var type = “ERROR”; //INFO, WARNING, ERROR

    var id = “213”; //Notification Id

    var time = 5000; //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    });

    }

  • Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: JavaScript Banner

    Put this code in your function at first place

    var accountId;

    if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) {

              accountId = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

    }

  • PeterJK Profile Picture
    PeterJK 45 on at
    RE: JavaScript Banner

    Pardon the ignorance here, but how would I implement that into the code above?

  • Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: JavaScript Banner

    Because you are not passing the accountId and this is undefined in the function. You have to retrieve the accountId from the lookup value of account

  • PeterJK Profile Picture
    PeterJK 45 on at
    RE: JavaScript Banner

    Naveen

    I created a Web Resource, input your Jscript into it, then created an onLoad event for the form referencing the function.

    After publishing, I get an undefined error prompt on form load.  Here is what I used,  not sure if I'm using the correct values:

    function serviceWatchCheck() {

    //execute the query and get the results

    Xrm.WebApi.retrieveRecord("account", accountId, "?$select=cmm_servicewatch").then(function (result) {

    //Show the warning message

    if (data["cmm_servicewatch"] === null && data["cmm_servicewatch"]==="Yes")

    {

    var message = “This Account Is Under Service Watch”;

    var type = “WARNING”;  //INFO, WARNING, ERROR

    var id = “123”;  //Notification Id

    var time = 5000;  //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    }

    }, function (error) {

    //show error message if something error occured during operation

    var message = error.message;

    var type = “ERROR”;  //INFO, WARNING, ERROR

    var id = “213”;  //Notification Id

    var time = 5000;  //Display time in milliseconds

    Xrm.Page.ui.setFormNotification(message, type, id);

    });

    }

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: JavaScript Banner

    Hello Peter,

    You can make use of below code snippet to show alert based on related entities record value. You only need to pass the account id which will you get from accounts lookup field:

    function showAlertOnAccountClassified() {
    //execute the query and get the results
    Xrm.WebApi.retrieveRecord("account", accountId, "?$select=servicewatch").then(function (result) {
    //Show the warning message
    if (data["servicewatch"] != null && data["servicewatch"]=="desiredValue")
     {

    var message = “Your message here.”;
    var type = “WARNING”;  //INFO, WARNING, ERROR
    var id = “123”;  //Notification Id
    var time = 5000;  //Display time in milliseconds
    Xrm.Page.ui.setFormNotification(message, type, id);

    }

    }, function (error) {

    //show error message if something error occured during operation

    var message = error.message;
    var type = “ERROR”;  //INFO, WARNING, ERROR
    var id = “213”;  //Notification Id
    var time = 5000;  //Display time in milliseconds
    Xrm.Page.ui.setFormNotification(message, type, id);


    });
    }

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: JavaScript Banner

    Hi Peter,

    Either using crm rest-builder you can retrieve account entity value in CRM javascript or else you can use Xrm.WebApi library method to retrieve account record.

    please refer below url for more info:

    carldesouza.com/.../

    ashwaniashwin.wordpress.com/.../

    carldesouza.com/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans