Back to Basics # 68: 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.

Step 2:

After Step 1, open any model driven app as an 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.

Step 3:

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

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

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.

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 .

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 can be found here

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

One thought on “Back to Basics # 68: Run JavaScript from Modern Command Designer in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s