Friday, 4 September 2015

Call Actions from Javascript/C# in CRM 2013/2015 and Trigger a plugin


Today we got a requirement to call a Plugin on click of Custom button.

First we thought of updating a field in the record and register a plugin on that field update. After some search I came to that we can trigger a plugin on execution of Action and also we can execute the Action from Javascript.

Follow the below steps to implement this.

Create a blank Action name Trigger Plugin.
Open Plugin Registration Tool and Register a plugin which you want to execute.
Create a step and give the message as Action Name.

That's it, you are done.
When you execute a Action, you will the plugin is triggered.
One more good thing is we can call the Action from JavaScript and also from C#.

OrganizationRequest orgReq = new OrganizationRequest("new_TriggerPlugin");
orgReq["Target"] = new EntityReference("account", new Guid("01DC64CE-0752-E511-80D8-000D3AA023B6"));
OrganizationResponse response = iService.Execute(orgReq);

function CallAction() {
    var accountId =;
    var entityName = "account";
    var requestName = "new_TriggerAction";
    ExecuteAction(accountId, entityName, requestName);

function ExecuteAction(entityId, entityName, requestName) {
    // Creating the request XML for calling the Action
    var requestXML = ""
    requestXML += "<s:Envelope xmlns:s=\"\">";
    requestXML += "  <s:Body>";
    requestXML += "    <Execute xmlns=\"\" xmlns:i=\"\">";
    requestXML += "      <request xmlns:a=\"\">";
    requestXML += "        <a:Parameters xmlns:b=\"\">";
    requestXML += "          <a:KeyValuePairOfstringanyType>";
    requestXML += "            <b:key>Target</b:key>";
    requestXML += "            <b:value i:type=\"a:EntityReference\">";
    requestXML += "              <a:Id>" + entityId + "</a:Id>";
    requestXML += "              <a:LogicalName>" + entityName + "</a:LogicalName>";
    requestXML += "              <a:Name i:nil=\"true\" />";
    requestXML += "            </b:value>";
    requestXML += "          </a:KeyValuePairOfstringanyType>";
    requestXML += "        </a:Parameters>";
    requestXML += "        <a:RequestId i:nil=\"true\" />";
    requestXML += "        <a:RequestName>" + requestName + "</a:RequestName>";
    requestXML += "      </request>";
    requestXML += "    </Execute>";
    requestXML += "  </s:Body>";
    requestXML += "</s:Envelope>";
    var req = new XMLHttpRequest();"POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "");
    //Get the Resonse from the CRM Execute method
    var response = req.responseXML;

You can use SoapLogger to generate request for you. Follow the link to know How to use SoapLogger

Hope this helps.

Happy CRM'ing


  1. This is very helpful code...Thanks

  2. step by step procedure actions call in JavaScript and plugin and workflow

  3. i followed the steps, but action is not triggering the plugin. can you please help