Tuesday, January 12, 2010

Convert lead to contact, account and/or opportunity programmatically

public void Convert(Guid leadId, string entityName)
{
SetStateLeadRequest qualifier = new SetStateLeadRequest();
qualifier.EntityId = leadId;
qualifier.LeadState = LeadState.Qualified;
qualifier.LeadStatus = -1;
Service.Execute(qualifier);
//lead is qualified, but not converted to contact yet, if you check the leads from user interface, you will not be able to see this lead in open leads, but you will not see it in contacts.

InitializeFromRequest req = new InitializeFromRequest();
req.EntityMoniker = new Moniker(); // this is the very thing that does the job.
req.EntityMoniker.Id = leadId;
req.EntityMoniker.Name = "lead";
req.TargetEntityName = entityName; //contact for our example req.
req.TargetFieldType = TargetFieldType.All;
InitializeFromResponse rps = (InitializeFromResponse)Service.Execute(req);
//now the lead is converted to a contact, and you can see it in contacts.
Guid entityId = service.Create(rps.Entity);
}

No comments:

Post a Comment