Friday 4 September 2015

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

Hi,

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#.

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

JavaScript
function CallAction() {
    var accountId = Xrm.Page.data.entity.getId();
    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=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestXML += "  <s:Body>";
    requestXML += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestXML += "      <request xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">";
    requestXML += "        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    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();
    req.open("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", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    req.send(requestXML);
    //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
Gopinath

3 comments:

  1. This is very helpful code...Thanks

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

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

    ReplyDelete