Sample Plugin code to run on Reactivate Case (Incident) in Dynamics 365

Just sharing a sample code that runs on the Activation of case (incident) record and updates one field in it.

We need to register the plugin in both SetState and SetStateDynamicEntity message. Interestingly what we found was the plugin runs perfectly even if it is registered only for SetStateDynamicEntity. It triggers properly either we update it from CRM UI or through the CRM SDK for SetStateDynamicEntity message.


protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new InvalidPluginExecutionException("localContext");
}

// TODO: Implement your custom Plug-in business logic.

var context = localContext.PluginExecutionContext;
var tracingService = localContext.TracingService;

if (context.InputParameters.Contains("EntityMoniker"))
{
tracingService.Trace("Inside Entity Moniker");

var targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
if (targetEntity.LogicalName == "incident")
{
tracingService.Trace("Inside Incident");

int state = ((OptionSetValue)context.InputParameters["State"]).Value;

tracingService.Trace("State = " + state);

// if status is active
if (state == 0)
{
tracingService.Trace("State Active");

// update one field in the record
Entity incident = new Entity("incident");
incident.Id = context.PrimaryEntityId;incident.Attributes["sab_fash_casestatusid"] = new EntityReference("sab_fashioncasestatus", new Guid("6F456BDC-CD7D-E811-813C-5065F38B15F2"));
localContext.OrganizationService.Update(incident);
tracingService.Trace("Record Updated");
}

}
}

}
}

Hope it helps..


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Sample Plugin code to run on Reactivate Case (Incident) in Dynamics 365”

Share your thoughts

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 )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.