MS Dynamics - Create and Auto qualify a lead from C#

Ankaprasad
Posted by Ankaprasad under C# category on | Points: 40 | Views : 433
 
//Create Lead and auto qualify the lead also create account ,contact ,opportunity
public static bool CreateLead(Lead leadDetails)
{
bool status = false;
try
{
IOrganizationService _service = CRMConnection.GetService();
Guid _leadAccountId = new Guid();
Guid _contactId = new Guid();
Guid _opportunityId = new Guid();
Guid _leadId = Guid.Empty;

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.Product] = new EntityReference("product", new Guid(leadDetails.Product));
lead[LeadAttributes.PriceList] = new EntityReference("pricelevel", new Guid("6CBD9E5B-840D-E911-A823-000D3A372AB9"));
lead[LeadAttributes.Rent] = new OptionSetValue(leadDetails.RentType);
lead[LeadAttributes.PropertyType] = new OptionSetValue(leadDetails.PolicyInputPropertyType);
lead[LeadAttributes.NoOfFamily] = new OptionSetValue(leadDetails.NumberOfFamily);
lead[LeadAttributes.City] = leadDetails.City;
lead[LeadAttributes.State] = new OptionSetValue(leadDetails.State);
lead[LeadAttributes.Country] = leadDetails.Country;
lead[LeadAttributes.ZipCode] = leadDetails.ZipCode;

_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;
}
}

status = true;
}
catch (Exception)
{
status = false;
}
return status;
}

}

public class LeadAttributes
{
public const string LeadSource = "leadsourcecode";
public const string FirstName = "firstname";
public const string LastName = "lastname";
public const string FullName = "fullname";
public const string Email = "emailaddress1";
public const string Mobile = "mobilephone";
public const string Product = "crmfi_product";
public const string Rent = "crmfi_rentorown";
public const string PropertyType = "crmfi_policyinputpropertytype";
public const string NoOfFamily = "crmfi_numberoffamilyunits";
public const string City = "address1_city";
public const string State = "crmfi_state";
public const string Country = "address1_country";
public const string ZipCode = "address1_postalcode";
public const string PriceList = "crmfi_pricelist";


}

Comments or Responses

Login to post response