Run JavaScript From Modern Command Designer In Dynamics CRM

Introduction

During certain scenarios we must call JavaScript from Command bar present in a ribbon for every Dynamics CRM Form. As an example, contact main form was used in a model driven app to explain this scenario.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Run Javascript from Modern CommandBar in Dynamics CRM Figure 1

Step 2

After Step 1, open any model driven app as example I have added contact form in my model driven app (Quick Find App) and navigated to contacts main form and added new command bar and name it as Run Java Script as shown in the figure.

Run Javascript from Modern CommandBar in Dynamics CRM

Step 3

After Step 2, open existing Webresource cr5bc_contactmainform javascript type as shown in the below figure.

Run Javascript from Modern CommandBar in Dynamics CRM

Step 4

After Step 3, open Webresource and paste the below code

ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function() {
        calledfrommoderncommandbar();
    },
    __namespace: true
}
function calledfrommoderncommandbar() {
    alert("called from modern command bar using javascript");
}

as shown in the below figure

Run Javascript from Modern CommandBar in Dynamics CRM

Step 5

After Step 4, save the Webresource and publish it and go the modern command editor of the canvas app here[ Example: Quick Find App ] and then click on the new command bar Run JavaScript and on the right side under library add library as cr5bc_contactmainform and under that field provide the method as ContosoVaccination.Scripts.ContactForm.handleOnLoad and save and publish it as shown in the below figure.

Run Javascript from Modern CommandBar in Dynamics CRM

Step 6

After Step 5, open any contact record and click on Run Javascript button, you should see alert “called from modern command bar using javascript” as shown in the below figure.

Run Javascript from Modern CommandBar in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload javascript file.
  2. Here I have concentrated on calling javascript not concentrated on how to create a command button on modern command bar in model driven app.
  3. Also observe, I have considered existing Webresource created earlier, you can also create new javascript Webresource.
  4. Observe, in code there was no execution context.
  5. Microsoft Documentation : Here

Conclusion

In this way, one can easily run javascript action from Modern Command Designer in Dynamics CRM.


Similar Articles