Click here to Skip to main content
15,894,017 members
Articles / Programming Languages / C#
Article

How to convert leads to an account or contact?

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
15 Jun 2007CPOL 37K   139   7   10
Gives a brief description to convert the leads in MS CRM 3.0.

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"   );
  // first, keep these kind of credential information in the config file, second, use the appropriate values for this statement to authenticate your service.
       }
}

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);
      //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.
}

This is it, now you can create an instance of the class and call the convert function anytime needed,

Have fun!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Tradesoft Business Services
Turkey Turkey
Microsoft Dynamics CRM Consultant

Comments and Discussions

 
QuestionGetting error - 3 is not a valid status code on contact Pin
Eddi Rae11-Jul-11 4:45
Eddi Rae11-Jul-11 4:45 
AnswerRe: Getting error - 3 is not a valid status code on contact Pin
Eddi Rae27-Jul-11 3:16
Eddi Rae27-Jul-11 3:16 
GeneralError using this code Pin
michelle sollicito13-May-11 4:47
michelle sollicito13-May-11 4:47 
Generalonly the system fields are getting mapped. Custom fields are not getting mapped. Pin
raghav.mohit7-Apr-10 1:39
raghav.mohit7-Apr-10 1:39 
GeneralI tried this example but getting the error... Pin
IMMujeeb14-Apr-09 4:00
IMMujeeb14-Apr-09 4:00 
Error: Message: Server was unable to process request.
StackTrace: at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at DANBImportService.CrmSdk.CrmService.Execute(Request Request) in D:\Asim\DANBImportService\Web References\CrmSdk\Reference.cs:line 122
at DANBImportService.importcsv.ConvertLead2Contact(Guid leadIdvalue) in D:\Asim\DANBImportService\importcsv.cs:line 519
at DANBImportService.importcsv.CreateDynamicEntity(DataRow entityData, Hashtable entityAttributes, CrmService _service) in D:\Asim\DANBImportService\importcsv.cs:line 556
TargetSite: System.Object[] ReadResponse(System.Web.Services.Protocols.SoapClientMessage, System.Net.WebResponse, System.IO.Stream, Boolean)
4/14/2009 9:41:31 AM ==> End Time: 4/14/2009 9:41:36 AM, Concurrent Threads: 1, Tput: 0, Duration(s): 0.406263


I'm getting this error when i add these lines to code
SetStateLeadRequest qualifier = new SetStateLeadRequest();
qualifier.EntityId = leadIdvalue;
qualifier.LeadState = LeadState.Qualified;
qualifier.LeadStatus = -1;
this._crmService.Execute(qualifier);


Otherwise, the code executes fine.

But the problem is that, the record still exists in Lead and contact is also added. Though i've to search the contact in contact list.
And if i convert the lead from CRM Interface, lead is converted correctly and moved to contact.

Can you help me out in this plz.

Thx,
Mujeeb.
GeneralRe: I tried this example but getting the error... Pin
IMMujeeb14-Apr-09 4:46
IMMujeeb14-Apr-09 4:46 
QuestionDoesn't show in contacts? Pin
mgrif25-Oct-07 5:04
mgrif25-Oct-07 5:04 
AnswerRe: Doesn't show in contacts? Pin
Cem Onvar11-Nov-07 22:20
Cem Onvar11-Nov-07 22:20 
AnswerRe: Doesn't show in contacts? Pin
berend13-Aug-09 3:12
berend13-Aug-09 3:12 
AnswerRe: Doesn't show in contacts? Pin
Nathan Greenway27-Jan-10 16:08
Nathan Greenway27-Jan-10 16:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.