Hi,
I worte a plugin for the campaign entity in CRM 2011and it's not working, and below are the rules we have (Campaign Status is a custom optionset field)
A) Rule Logic – De-Active Campaign
If the ‘Campaign Status’ has a value of ‘Proposed’, ‘Approved’, or ‘Completed’ and the User selects to ‘De-Active’ the campaign, display an error message to the user:
B) RULE Logic – Entered in Error
If the User selects the ‘Campaign Status’ value of ‘Entered in Error’, De-Active the campaign upon ‘Save’.
Here's the code, I registered the plugin against Campaign with Pre Operation SetState and Pre Operation SetStateDynamicEntity messages.
public class PreSetStateCampaignPlugin:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Extract the tracing service
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
SqlConnHelper sqlHelper = new SqlConnHelper(context.OrganizationName, context.InitiatingUserId);
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService organizationService = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents a campaign. If not, this plug-in was not registered correctly.
if (entity.LogicalName != "campaign")
return;
if (entity.Attributes.Contains("cig_campaignstatuscode") && entity.Attributes["cig_campaignstatuscode"] != null)
{
OptionSetValue campaignStatusCode = (OptionSetValue)entity.Attributes["cig_campaignstatuscode"];
if (campaignStatusCode !=null && campaignStatusCode.Value == 100000004)
{
updateCampaignState(organizationService, entity.Id, 1);
}
if(campaignStatusCode != null && (campaignStatusCode.Value == 100000000 || campaignStatusCode.Value == 100000001 || campaignStatusCode.Value == 100000002) )
{
OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];
if (state.Value == 1 && status.Value == 6)
{
throw new InvalidOperationException("Cannot deactivate a campaign with a Proposed/Approved/Completed Campaign Status");
}
}
}
}
catch (Exception)
{
throw;
}
}
}
private void updateCampaignState(IOrganizationService organizationService, Guid campaignId, int state)
{
SetStateRequest request = new SetStateRequest();
request.EntityMoniker = new EntityReference();
request.EntityMoniker.Id = (Guid)campaignId;
request.EntityMoniker.LogicalName = "campaign";
request.State = new OptionSetValue(state);
request.Status = new OptionSetValue(6);
organizationService.Execute(request);
}
}