Introduction
Hi,
A few days ago i needed to convert some lead entities to contact or account by server side programming.
I thought this could be done easily by using TargetRelatedRequest and response classes mentioned in CRM 3.0 SDK, but i found out that would not cover all my need,
If you seek to do the same operation of converting the lead as done on the user interface, here we go,
Using the code
Before starting, I assume you already added the crm web service reference in your project, otherwise you will not be able to reach the relevant classes.
class LeadConverter
{
private CrmService _service;
public CrmService Service
{
get { return _service; }
set { _service = value; }
}
public LeadConverter()
{
Service = new CrmService();
Service.Credentials = new System.Net.NetworkCredential( "username" , "password" , "domain" );
}
}
Well, above is the infrastructure of our object, now we add the convert function to accomplish our task.
public void Convert(Guid leadId, string entityName)
{
SetStateLeadRequest qualifier = new SetStateLeadRequest();
qualifier.EntityId = leadId;
qualifier.LeadState = LeadState.Qualified;
qualifier.LeadStatus = -1;
Service.Execute(qualifier);
InitializeFromRequest req = new InitializeFromRequest();
req.EntityMoniker = new Moniker();
req.EntityMoniker.Id = leadId;
req.EntityMoniker.Name = "lead";
req.TargetEntityName = entityName;
req.TargetFieldType = TargetFieldType.All;
InitializeFromResponse rps = (InitializeFromResponse)Service.Execute(req);
}
This is it, now you can create an instance of the class and call the convert function anytime needed,
Have fun!