{Code Tip} How to set Global Notification by using JavaScript

In this blog post, we will learn a new code snippet of JavaScript that we are using to implement a requirement in a Model Driven App. The feature is called “addGlobalNotification()”, and it allows us to show global notifications for an app.

Requirement:

To show global notifications for an app when a model-driven app gets open then show Information notification.

We are using the “addGlobalNotification” client API to display notifications globally in our model-driven apps. These notifications can be error messages, information, warnings, or success messages.

Implementation:

We are implementing this requirement by using the JavaScript                                        

Xrm.App.addGlobalNotification(notification).then(successCallback, errorCallback);

The ‘Xrm.App.addGlobalNotification()’ method is used in Dynamics 365 to display global notifications to the user. It can show different types of notifications such as errors, information, warnings, or success messages.

The method takes a ‘notification’ object as a parameter, which defines the notification’s message and type.

var notification = {
    type: 2,
    level: 4, //information
    message: "Greetings! Let's achieve great things together with Dynamics 365."
}
//To show global notification call client API - "addGlobalNotification()"
Xrm.App.addGlobalNotification(notification).then(
    function success(result) {
    console.log("Notification created with ID: " + result);
    return false;
},
    function (error) {
    console.log(error.message);
    return false;
});

Notification property:

The notification object contains the following properties.

  • action: An object specifying an action label and an event handler function that is called when the action label is clicked.
  • level: Indicates the level of importance of the notification, such as error (2), warning (3), or information (4).
  • type:  Defines the type of notification. Currently, only a value of 2 is supported, which displays a message bar at the top of the app.
  • message: The text message that will be displayed in the notification.
  • showCloseButton (optional): Determines whether a close button is shown, allowing the user to dismiss the notification.

To call the JavaScript, we are going to create a ribbon button, which we named as “Global notification”. there is a limitation in JavaScript that we cannot run JavaScript on the load of the app.

  • Here we have added “Custom Rule”.
  • Select JavaScript web resource that you have created, and add the function name.
  • Publish the button and open the Model-driven app to see global notification.

Output:

  • As you can see the Global Notification is visible on load of sample application in the GIF given below.

Hope it helps! 

Power 365ing as usual! 

Any requirements, implementation or consulting work in Power Platform or Dynamics 365 – end user, Microsoft partner or an individual? 

Problem Area – Technical, Functional, Training, Development or consulting? 

Me and my team are here to assist, please fill the following form for your business needs: Click here