Sometimes we need to trigger a Plugins while a record is
open, but we face a lots of issue. So here I am explaining how we can trigger
our Plugin on event of any record open.
There are major three things which help us:-
Plugin
Action
Action
JavaScript web resource
Let us discuss with an real world example:- I want to create
an activity at the time of contact record is open with the help of a Plugin.
So first I will create an Action in MS Dynamics
Setting => Customization=>
Customize the System => Processes
Add new Process
Put a name and select Category as Action and Entity as None (global).
Click ok. Now we can add Argument if we need to pass on Plugin. Here I am adding to
Arguments- ActivityName and ActivityDescription. (I will pass value
from my JavaScript code).
Now Save it, Publish it and Activate it.
Note: - Here Unique Name of my action will be displayed
on Plugin Registration Tool as a message.
Now I will write a
Plugin Like:-
Code:-
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PluginCreateActivity
{
public class CreateActivity : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service =
factory.CreateOrganizationService(context.UserId);
IOrganizationService service1 = (IOrganizationService)serviceProvider.GetService(typeof(IOrganizationService));
if (context.InputParameters != null)
{
string ActivityName =
context.InputParameters.Contains("ActivityName") ? (string)context.InputParameters["ActivityName"] : "";
string ActivityDescription =
context.InputParameters.Contains("ActivityDescription") ? (string)context.InputParameters["ActivityDescription"] : "";
Entity task
= new Entity("task");
task["subject"] =
ActivityName;
task["description"] =
ActivityDescription;
task["regardingobjectid"] = new EntityReference("contact",new Guid("336B5DF8-DDA9-E811-A965-000D3AF06236"));
service.Create(task);
}
}
}
}
Now register the Plugin and the add step like:-
Message = Unique Name
of Action.
Primary Entity=
none.
Secondary Entity=
none.
Now the Third Step is Add a javascript code as Web resource like:-
Setting => Customization=>
Customize the System => Web Resources
Add new. Put a Name,
Type as Script (JScript). And paste below JavaScript code in Text Editor.
Code:-
//Main Function
//fn Proceed To Call Action and Plugin
function fnProceedToCallActionPlugin() {
ActivityName = "sam test Activity";
ActivityDescription = "This is a test of an Action activity, while opening a
record."
fnRequestToCallaPlugin(ActivityName, ActivityDescription);
}
///fn Request To Call a Plugin
function
fnRequestToCallaPlugin(ActivityName, ActivityDescription) {
try{
// 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>ActivityName</b:key>";
requestXML += "
<b:value i:type=\"c:string\"
xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + ActivityName + "</b:value>";
requestXML += "
</a:KeyValuePairOfstringanyType>";
requestXML += "
<a:KeyValuePairOfstringanyType>";
requestXML += "
<b:key>ActivityDescription</b:key>";
requestXML += "
<b:value i:type=\"c:string\"
xmlns:c=\"http://www.w3.org/2001/XMLSchema\">" + ActivityDescription + "</b:value>";
requestXML += "
</a:KeyValuePairOfstringanyType>";
requestXML += "
</a:Parameters>";
requestXML += "
<a:RequestId i:nil=\"true\" />";
requestXML += "
<a:RequestName>" + "new_My_Record_Open_Action" + "</a:RequestName>";
requestXML += "
</request>";
requestXML += "
</Execute>";
requestXML += "
</s:Body>";
requestXML += "</s:Envelope>";
var req = new XMLHttpRequest();
req.open("POST", fnGetClientUrl(), 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.xml;
if (req.status === 200) {
fnRetriiveAfterSuccess();
} else {
alert("Problem in plugin call");
}
}
catch (err) {
alert(err);
}
}
//fn Retriive After Success
function fnRetriiveAfterSuccess() {
alert("Plugin called successfully");
}
//fn Get Client Url
function fnGetClientUrl() {
return Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web"
}
Now Save it and Publish it.
After that we need to add this on Form. So I am adding on Contact
Form.
Setting => Customization=>
Customize the System => Entities => Contacts => Forms => Select
Information Form (a default main form).
In Form Properties add this Web Resource(As shown in below
Screenshot).
Now Save the Form and Publish it.
Now go to Contact and open any Contact. Hope Plugin will
trigger.
Now I am going to check in activity.
Plugin triggered successfully.
Hope this Article will helpful for you.