Tags

, , , ,

Hello Everyone,

Recently I had a requirement wherein I was suppose to Qualify the Lead, Create associated Account and update the newly Created account with New assigned AccountNumber through a Plugin.

This can be achieved through QualifyLeadRequest Class.

For using this class you need to Add reference to Microsoft.Crm.Sdk.Proxy.dll
The Namespace used is: Microsoft.Crm.Sdk.Messages

  • I first found the Lead with specified LeadNo with the following FetchXml:
 var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
 fetchXml += "<entity name='lead'>";
 fetchXml += "<attribute name='leadid' />";
 fetchXml += "<filter type='and'>";
 fetchXml += "<condition attribute='new_leadno' operator='eq' value='" + LeadNO + "' />";
 fetchXml += "<condition attribute='statecode' operator='eq' value='0' />";
 fetchXml += "</filter>";
 fetchXml += "</entity>";
 fetchXml += "</fetch>";
var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
  • Then Using QualifyLeadRequest, the Specific Lead got Qualified and also gave the option of creating related Account.
QualifyLeadRequest req = new QualifyLeadRequest();
 req.LeadId = new EntityReference("lead", leadID);
 req.Status = new OptionSetValue(3);
 req.CreateAccount = true;
 QualifyLeadResponse res = (QualifyLeadResponse)service.Execute(req);
  • Now the final step was to find the newly created Account with the Qualified Lead and update the Account Number. It was possible through the following:
foreach (var entity in res.CreatedEntities)
 {
 if (entity.LogicalName == "account")
 {
 Entity acct = service.Retrieve("account", entity.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
 
 acct.Attributes["accountnumber"] = AN;
 
 service.Update(acct);
 }
 }

 

  • So now my final code looks something similar :
using Microsoft.Crm.Sdk.Messages;

 var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
 fetchXml += "<entity name='lead'>";
 fetchXml += "<attribute name='leadid' />";
 fetchXml += "<filter type='and'>";
 fetchXml += "<condition attribute='new_leadno' operator='eq' value='" + LeadNO + "' />";
 fetchXml += "<condition attribute='statecode' operator='eq' value='0' />";
 fetchXml += "</filter>";
 fetchXml += "</entity>";
 fetchXml += "</fetch>";
var result = service.RetrieveMultiple(new FetchExpression(fetchXml));
 if (result.Entities.Count > 0)
 {
 Guid leadID = ((Guid)result.Entities[0].Attributes["leadid"]);
QualifyLeadRequest req = new QualifyLeadRequest();
 req.LeadId = new EntityReference("lead", leadID);
 req.Status = new OptionSetValue(3);
 req.CreateAccount = true;
 QualifyLeadResponse res = (QualifyLeadResponse)service.Execute(req);
foreach (var entity in res.CreatedEntities)
 {
 if (entity.LogicalName == "account")
 {
 Entity acct = service.Retrieve("account", entity.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
 
 acct.Attributes["accountnumber"] = AN;
 
 service.Update(acct);
 }
 }
}

 

Hope this was quite helpful.

Thanks !!!
🙂

Advertisements