MS Dynamics - Create and auto qualify a lead from API

Ankaprasad
Posted by Ankaprasad under C# category on | Points: 40 | Views : 360
 
Entity lead = new Entity("lead");
lead[LeadAttributes.LeadSource] = new OptionSetValue(Convert.ToInt32(leadDetails.LeadSource));
lead[LeadAttributes.FirstName] = leadDetails.FName;
lead[LeadAttributes.LastName] = leadDetails.LName;
lead[LeadAttributes.FullName] = leadDetails.FName + "" + leadDetails.LName;
lead[LeadAttributes.Email] = leadDetails.Email;
lead[LeadAttributes.Mobile] = leadDetails.Mobile;
lead[LeadAttributes.City] = leadDetails.City;
lead[LeadAttributes.State] = new OptionSetValue(leadDetails.State);
lead[LeadAttributes.Country] = leadDetails.Country;
lead[LeadAttributes.ZipCode] = leadDetails.ZipCode;
Guid _leadId = _service.Create(lead);

//Auto qualify the Lead and create Account, contact and Opportunity

var qualifyIntoAccountContactReq = new QualifyLeadRequest();
qualifyIntoAccountContactReq["CreateAccount"] = true;
qualifyIntoAccountContactReq["CreateContact"] = true;
qualifyIntoAccountContactReq["CreateOpportunity"] = true;
qualifyIntoAccountContactReq["LeadId"] = new EntityReference("lead", _leadId);
qualifyIntoAccountContactReq["Status"] = new OptionSetValue(3);
var qualifyIntoAccountContactRes = (QualifyLeadResponse)_service.Execute(qualifyIntoAccountContactReq);
foreach (var entity in qualifyIntoAccountContactRes.CreatedEntities)
{
if (entity.LogicalName == "account")
{
_leadAccountId = entity.Id;
}
else if (entity.LogicalName == "contact")
{
_contactId = entity.Id;
}
else if (entity.LogicalName == "opportunity")
{
_opportunityId = entity.Id;
}
}

Comments or Responses

Login to post response