Code: Select all
namespace CrmVSSolutionIMS.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
/// <summary>
/// PostLeadUpdate Plugin.
/// Fires when the following attributes are updated:
/// All Attributes
/// </summary>
public class PostLeadUpdate: Plugin
{
/// <summary>
/// Alias of the image registered for the snapshot of the
/// primary entity's attributes before the core platform operation executes.
/// The image contains the following attributes:
/// No Attributes
/// </summary>
private readonly string preImageAlias = "PreImage";
/// <summary>
/// Alias of the image registered for the snapshot of the
/// primary entity's attributes after the core platform operation executes.
/// The image contains the following attributes:
/// No Attributes
///
/// Note: Only synchronous post-event and asynchronous registered plug-ins
/// have PostEntityImages populated.
/// </summary>
private readonly string postImageAlias = "PostImage";
/// <summary>
/// Initializes a new instance of the <see cref="PostLeadUpdate"/> class.
/// </summary>
public PostLeadUpdate()
: base(typeof(PostLeadUpdate))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "lead", new Action<LocalPluginContext>(ExecutePostLeadUpdate)));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
/// <summary>
/// Executes the plug-in.
/// </summary>
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
/// <see cref="IPluginExecutionContext"/>,
/// <see cref="IOrganizationService"/>
/// and <see cref="ITracingService"/>
/// </param>
/// <remarks>
/// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
/// The plug-in's Execute method should be written to be stateless as the constructor
/// is not called for every invocation of the plug-in. Also, multiple system threads
/// could execute the plug-in at the same time. All per invocation state information
/// is stored in the context. This means that you should not use global variables in plug-ins.
/// </remarks>
protected void ExecutePostLeadUpdate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
// TODO: Implement your custom Plug-in business logic.
try
{
localContext.Trace("Start");
Entity inputParameters = null;
// Check if the input parameters property bag contains a target
// of the create operation and that target is of type Entity.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target business entity from the input parameters.
inputParameters = (Entity)context.InputParameters["Target"];
// Verify that the entity represents a contact.
if (inputParameters.LogicalName != Models.Lead.EntityLogicalName) { return; }
}
else
{
throw new InvalidPluginExecutionException("No inputParameters");
}
if (inputParameters.Contains("leadqualitycode"))
{
OptionSetValue rating = ((OptionSetValue)(inputParameters["leadqualitycode"]));
if (rating.Value != 1 && rating.Value != 2)
{
return;
}
}
else
{
return;
}
Models.ims_initialassetscanreport initialScanReport = new Models.ims_initialassetscanreport();
initialScanReport.ims_name = string.Format("Initial Scan Report: {0}" , postImageEntity.Attributes["subject"]);
int notStarted = 176390003;
initialScanReport.ims_Status = new OptionSetValue(notStarted);
if (postImageEntity != null)
{
initialScanReport.ims_ims_lead_initialassetscanreportId = new EntityReference(Models.Lead.EntityLogicalName, postImageEntity.Id);
}
else
{
throw new InvalidPluginExecutionException("No postImageEntity");
}
initialScanReport.Id = localContext.OrganizationService.Create(initialScanReport);
Models.Lead lead = (Models.Lead)localContext.OrganizationService.Retrieve(Models.Lead.EntityLogicalName, inputParameters.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(new string[]{"ims_initialassetscanreportid"}));
if (lead != null && lead.ims_InitialAssetScanReportId == null)
{
lead.ims_InitialAssetScanReportId = new EntityReference(Models.ims_initialassetscanreport.EntityLogicalName, initialScanReport.Id);
localContext.OrganizationService.Update(lead);
}
localContext.Trace("End");
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message , ex);
}
}
}
}