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 += ;
    requestXML +=   ;
    requestXML +=     ;
    requestXML +=       ;
    requestXML +=         ;
    requestXML +=          
;
    requestXML +=             Target;
    requestXML +=             ;
    requestXML +=              
+ entityId + ;
    requestXML +=               + entityName + ;
    requestXML +=               ;
    requestXML +=             
;
    requestXML +=          
;
    requestXML +=         ;
    requestXML +=         ;
    requestXML +=        
+ requestName + ;
    requestXML +=       ;
    requestXML +=     ;
    requestXML +=  
;
    requestXML +=
;
    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 thoughts on “Call Actions from Javascript/C# in CRM 2013/2015 and Trigger a plugin

  1. Unknown January 19, 2017 / 12:24 pm

    This is very helpful code…Thanks

    Like

  2. MSCRM world February 8, 2017 / 3:46 am

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

    Like

  3. NEERAJ D KRIPLANI July 10, 2017 / 12:54 pm

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

    Like

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 )

Google photo

You are commenting using your Google 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