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);
}
// 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
This is very helpful code…Thanks
LikeLike
step by step procedure actions call in JavaScript and plugin and workflow
LikeLike
i followed the steps, but action is not triggering the plugin. can you please help
LikeLike