Recently we had a requirement to capture the date when a lead is qualified. Here we can create a new date time attribute to capture the lead qualification date and update this field either through workflow, flow, or plugin on lead qualification.
Status Code = 3 (Qualified)
We implemented a plugin on PreOperation – Update of lead, with filtering attributes as the state code.
Below is the sample code for the plugin –
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
try
{
tracingService.Trace("start plugin execution: {0}", this.GetType().FullName);
// plugin is in pre update stage of lead
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity &&
context.MessageName.Equals("update", StringComparison.OrdinalIgnoreCase) &&
context.Stage == 20)
{
var leadEntity = (Entity)context.InputParameters["Target"];
if(leadEntity.LogicalName == "lead")
{
if(leadEntity.Attributes.Contains("statuscode") &&
((OptionSetValue)leadEntity.Attributes["statuscode"]).Value == 3
)
{
tracingService.Trace("Lead with Id {0} is getting qualified at {1}",
leadEntity.Id.ToString(), DateTime.UtcNow);
// the custom date time field to capture the qualification date
leadEntity["custom_qualificationdate"] = DateTime.UtcNow;
}
}
}
tracingService.Trace("end plugin execution: {0}", this.GetType().FullName);
}
catch (System.ServiceModel.FaultException<OrganizationServiceFault> ex)
{
tracingService.Trace(ex.Detail.Message);
throw;
}
catch (Exception ex)
{
tracingService.Trace(ex.ToString());
throw;
}
}
Hope it helps..