Execute a plugin from JavaScript in Microsoft Dynamics 365 Customer Engagement (CRM)

Many-a-times we face such situations where we need to call a plugin from a form event of javascript or a ribbon button.


Below are some of the ways of doing it:

  • Keep a flag field on form and update that field. Call plugin on the update of that   form
  • Create a dummy entity of type activity and create a record using javascript everytime you want to call the plugin. On creation of this record of dummy entity, call your plugin
  • Create an action in crm and register your plugin step on action message using plugin registration tool. Call this action using javascript.

You probably would have used the 1st approach many times which is the easiest and quickest solution, but this solution have drawbacks of it’s own.


Let’s list out the characteristics of each of the above 3 approaches:

1. Keeping a flag approach

  • Easy and quick to build
  • Need to create an unnecessary field in crm
  • Auditing is possible on the flag field to identify who and when triggered the plugin

2. Creating a dummy entity approach

  • Need to create a new dummy entity
  • On every fire of step, one record is created
  • Auditing is possible on the dummy entity record to identify who and when triggered the plugin

3. Creating and calling an action approach

  • No need to create any extra field or entity
  • Auditing is not possible directly 

In this post, we’ll discuss the 3rd approach i.e. Calling a plugin form javascript using an action as it’s unique and have some benefits over rest. 


On account, we have a submit for approval button, on click of which we want to fire a plugin where we want to do some task (eg: sending an account for further approval) and change the status of the account to submitted.

Step 1: Create an action

Go to Settings>Processes and click new to create a new process. 

Select category as action, the entity as account and click ok as shown below:

Create an action


Step 2: Activate the action

Once created, your action would look like below image. It’s just a blank action with no arguments or steps. Click on the activate button to activate it. It’s unique name is new_Submitaccountforapproval. You can keep the name as per your need. This name will look as a message in plugin registration tool to register a step upon.


Activate the Action


Step 3: Build the plugin

Now write your plugin code on account entity as below

using System;
using Microsoft.Xrm.Sdk;
namespace TestPlugin
  public class AccountApproval : IPlugin
   public void Execute(IServiceProvider serviceProvider)
   #region Setup
   IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
   IOrganizationService service = ((IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory))).CreateOrganizationService(new Guid?(context.UserId));
   ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
   if ((context.InputParameters.Contains("Target")) && (context.InputParameters["Target"] is Entity))
    Entity entity = (Entity)context.InputParameters["Target"];
    if (entity.LogicalName.ToLower() != "account") return;
      //perfrom some operation here
      //change the status to submitted
      entity["statuscode"] = new OptionSetValue(100000000);service.Update(entity);
     catch (Exception ex)
      throw new Exception(ex.Message);

Step 4: Register the plugin assembly and step

Register your plugin assembly and register your step on new_Submitaccountforapproval message.

Select the primary entity as account and stage as post operation as shown below and register the step.


Register New Step


Step 5: javacsript to Call action 

On successful registration of plugin step, it’s the time to call your action using javascript.

Below is the javascript code to call the new_Submitaccountforapproval action. You need to change the name of action being called in you javascript code if your action name is different. Call this CallAction function on the javascript event or the ribbon button click where ever you want.

function CallAction()
  var currentRecordIdString = Xrm.Page.data.entity.getId();
  var currentRecordId = currentRecordIdString.replace("{", '').replace("}", '');
  var query = "accounts(" + currentRecordId + ")/Microsoft.Dynamics.CRM.new_Submitaccountforapproval";
  var req = new XMLHttpRequest();
  var url = Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query;
  req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" + query, false);
  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 == 204) 
    var error = JSON.parse(this.response).error;

Step 6: Creating a button and calling above js function on click of that button

i) Create a solution having account entity and a javascript library having above function (new_plugintest for us) as shown below:


Creating a Solution


ii)  Navigate to settings>solution and click on ribbon work bench icon as shown below



iii) Select the solution we just created and click ok



iv) Drag a button to the form. Change it’s name to Submit as shown in below screenshot:



v) Click on + sign to create a command and click on add action  in the action tab to add a javascript action as shown below:



vi) Select the js library (new_pluginstest in our case) and set function name (callaction in our case) as shown below:



vii) Under buttons, select the existing submit button and select the command we just created on command in the property tab as shown below:



viii) Click on publish button and click ok to save and publish changes:



ix) Once published, submit button will be visible on account form.



Step 6: Testing


For testing, click on the submit button on which we have called CallAction function and if everything is correct, you’ll get results as below:



Status changed to submitted



So this is how you can utilize crm actions to call a plugin from javascript without creating extra fields or entity.


About Magnifez Technologies

Magnifez Technologies is a Certified Microsoft Partner providing technology and consulting services to enterprise companies in a wide range of technologies like Dynamics 365 Customer Engagement (CRM) and Dynamics 365 Unified Operations (ERP) applications.

We are focused on providing vertical industry solutions built on the top of Microsoft Dynamics applications. Our mission is to help clients grow in a competitive business environment using powerful, user friendly and affordable enterprise software solutions which swiftly respond to the changing nature of the industry.

Leave a Reply