SetState Dynamic Entity – Using MS CRM 2011

SetStae For Entity :

For e.g. While Reactivate the Incident Entity “SetState”

Register this Plugin in Both “SetState” and “SetStateDynamicEntity” on incident entity

Entity caseEntity;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

try
{
if (context.InputParameters.Contains(“EntityMoniker”))
{
// Work with the Moniker
EntityReference targetEntity = (EntityReference)context.InputParameters[“EntityMoniker”];
if (targetEntity.LogicalName != “incident”)
{ return; }
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

switch (context.MessageName)
{
// Check if entity status is changed
case “SetStateDynamicEntity”:
if (context.InputParameters.Contains(“State”))
{
EntityReference CaseEntityRef = (EntityReference)context.InputParameters[“EntityMoniker”];

if (((OptionSetValue)context.InputParameters[“State”]).Value == 0)
{

U can write any logic here

}
}
break;
}
service.Update(caseEntity);
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(“An error occured for SetState plugin ” + e.Message + e.InnerException);
}

}

 

Cheers,

Advertisements

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 )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s