How to Get the records when Lead is Qualified in Microsoft Dynamics CRM using Plugin C#
In this blog I am going to explain and show an example when a lead is Qualified then how we can get the new records or we can update records in newly created record when Lead status is Qualified.
1. Register your created plugin in "QualifyLead" message and On Post-Operation to get the records when lead is Qualified. ::
2. Here in this example I am setting up a Lookup field value in newly created Opportunity when lead is Qualified. Just type the code as below and register as said in Step 1.
IOrganizationService serviceDefaultUser = null;
IPluginExecutionContext contextPlugin = GetContext(serviceProvider);
serviceDefaultUser = GetServiceObject(serviceProvider, contextPlugin);
// Get the qualified lead.
EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];
Entity lead = orgSrvService.Retrieve("lead", leadid.Id, new ColumnSet(true));
EntityReference erLookUpField = GetLookupValueFromCustomEntity(orgSrvService);
foreach (EntityReference erCreatedEntity in ((IEnumerable)context.OutputParameters["CreatedEntities"]))
{
//Check if Opportunity record is created when lead qualified.
if (erCreatedEntity.LogicalName == "opportunity")
{
Entity entOpportunity = orgSrvService.Retrieve("opportunity", erCreatedEntity.Id, new ColumnSet(true));
entOpportunity["new_lookupid"] = erLookUpField;
orgSrvService.Update(entOpportunity);
}
}
1. Register your created plugin in "QualifyLead" message and On Post-Operation to get the records when lead is Qualified. ::
2. Here in this example I am setting up a Lookup field value in newly created Opportunity when lead is Qualified. Just type the code as below and register as said in Step 1.
IOrganizationService serviceDefaultUser = null;
IPluginExecutionContext contextPlugin = GetContext(serviceProvider);
serviceDefaultUser = GetServiceObject(serviceProvider, contextPlugin);
// Get the qualified lead.
EntityReference leadid = (EntityReference)context.InputParameters["LeadId"];
Entity lead = orgSrvService.Retrieve("lead", leadid.Id, new ColumnSet(true));
EntityReference erLookUpField = GetLookupValueFromCustomEntity(orgSrvService);
foreach (EntityReference erCreatedEntity in ((IEnumerable)context.OutputParameters["CreatedEntities"]))
{
//Check if Opportunity record is created when lead qualified.
if (erCreatedEntity.LogicalName == "opportunity")
{
Entity entOpportunity = orgSrvService.Retrieve("opportunity", erCreatedEntity.Id, new ColumnSet(true));
entOpportunity["new_lookupid"] = erLookUpField;
orgSrvService.Update(entOpportunity);
}
}
private IOrganizationService GetServiceObject(IServiceProvider serviceProvider, IPluginExecutionContext contextPlugin)
{
IOrganizationServiceFactory factory = null;
IOrganizationService service = null;
if (IsObjectNull(serviceProvider) || IsObjectNull(context))
return null;
factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = factory.CreateOrganizationService(context.UserId);
return service;
}
private IPluginExecutionContext GetContext(IServiceProvider serviceProvider)
{
if (IsObjectNull(serviceProvider))
return null;
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
return context;
}
public static bool IsObjectNull(Object obj)
{
if (obj == null)
return true;
else
return false;
}
If you need attribute values from lead Entity then use the Fetch Query to get all required lead attributes as below::
private Entity GetLead(Guid guid, IOrganizationService serviceDefaultUser)
{
Entity entLead = null;
string sLeadXML = @" <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='lead'>
<attribute name='fullname' />
<attribute name='statuscode' />
<attribute name='address1' />
<attribute name='statecode' />
<attribute name='city' />
<filter type='and'>
<condition attribute='leadid' operator='eq' value='" + guid + @"' />
</filter>
</entity>
</fetch>";
EntityCollection ecLead = QueryByFetchXml(serviceDefaultUser, sLeadXML);
if (ecLead.Entities.Count > 0)
{
entLead = ecLead.Entities[0];
}
else
{
return null;
}
return entLead;
}
public static bool IsStringValueNullOrBlank(string strValue)
{
if (strValue == null || strValue == string.Empty)
return true;
else
return false;
}
{
EntityCollection entities = new EntityCollection();
if (IsObjectNull(service) || IsStringValueNullOrBlank(xmlString))
return null;
entities = service.RetrieveMultiple(new FetchExpression(xmlString));
return entities;
}
Comments
Post a Comment