In MS CRM 2011 we have a common requirement for plugins setstate message i.e when the status of a record changes (like activate/deactivated change of a record etc.)
So, here we will look at some plugin codes which executes to update certain field when a record state changes ie activated/deactivated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
namespace SetState
{
public class SetState:IPlugin
{
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
var targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
if (targetEntity.LogicalName != "new_activeacc")
{ return; }
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity ac = service.Retrieve("new_activeacc", context.PrimaryEntityId, new ColumnSet(true));
ac.Attributes["new_checkamt"] = 0;
service.Update(ac);
}
}
catch(Exception e)
{
throw new InvalidPluginExecutionException("An error occured for SetState plugin "+e.Message + e.InnerException);
}
}
}
}
Till now, the plugin code is perfect.
But we need to take some points into consideration while registering the plugin for setstate message
For the above plugin to work we need to register the plugin for both setstate and setstatedynamic message.
The reason why both messages are required is both messages perform the same action in CRM and as there is no typical thumbrule for which action setstate/setstatedynamic mesage is fired,its better to register the plugin for both messages so that our plugin works properly.
Another Example
Here is a sample plugin that would prevent an inactive contact record from getting activated.
Plugin has been registered against contact entity and two steps have been registered one on setstate and other on setstatedynamicentity messages on Pre event.
public void Execute(IPluginExecutionContext context)
{
// In case of SetState and SetStateDynamicEntity message InputParameter
// would contain EntityMoniker parameter
Moniker entity = null;
if (context.InputParameters.Properties.Contains("EntityMoniker") &&
context.InputParameters.Properties["EntityMoniker"] is Moniker)
{
entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];
// Get the state to which record is to be changed
// If Active the record is being activated
// If Inactive the record is being deactivated
string state=
(string)context.InputParameters.Properties[ParameterName.State];
// Verify that the entity represents an account.
if (entity.Name == EntityName.contact.ToString() && state=="Active")
{
throw new InvalidPluginExecutionException("Record can't be activated");
}
}
}
So, here we will look at some plugin codes which executes to update certain field when a record state changes ie activated/deactivated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
namespace SetState
{
public class SetState:IPlugin
{
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
var targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
if (targetEntity.LogicalName != "new_activeacc")
{ return; }
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
Entity ac = service.Retrieve("new_activeacc", context.PrimaryEntityId, new ColumnSet(true));
ac.Attributes["new_checkamt"] = 0;
service.Update(ac);
}
}
catch(Exception e)
{
throw new InvalidPluginExecutionException("An error occured for SetState plugin "+e.Message + e.InnerException);
}
}
}
}
Till now, the plugin code is perfect.
But we need to take some points into consideration while registering the plugin for setstate message
For the above plugin to work we need to register the plugin for both setstate and setstatedynamic message.
The reason why both messages are required is both messages perform the same action in CRM and as there is no typical thumbrule for which action setstate/setstatedynamic mesage is fired,its better to register the plugin for both messages so that our plugin works properly.
Another Example
Here is a sample plugin that would prevent an inactive contact record from getting activated.
Plugin has been registered against contact entity and two steps have been registered one on setstate and other on setstatedynamicentity messages on Pre event.
public void Execute(IPluginExecutionContext context)
{
// In case of SetState and SetStateDynamicEntity message InputParameter
// would contain EntityMoniker parameter
Moniker entity = null;
if (context.InputParameters.Properties.Contains("EntityMoniker") &&
context.InputParameters.Properties["EntityMoniker"] is Moniker)
{
entity = (Moniker)context.InputParameters.Properties["EntityMoniker"];
// Get the state to which record is to be changed
// If Active the record is being activated
// If Inactive the record is being deactivated
string state=
(string)context.InputParameters.Properties[ParameterName.State];
// Verify that the entity represents an account.
if (entity.Name == EntityName.contact.ToString() && state=="Active")
{
throw new InvalidPluginExecutionException("Record can't be activated");
}
}
}
No comments:
Post a Comment