Click here to Skip to main content
Licence CPOL
First Posted 15 Jun 2007
Views 13,994
Downloads 84
Bookmarked 7 times

How to convert leads to an account or contact?

By | 15 Jun 2007 | Article
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)

About the Author

Cem Onvar

Software Developer (Senior)
Tradesoft Business Services
Turkey Turkey

Member

Microsoft Dynamics CRM Consultant

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionGetting error - 3 is not a valid status code on contact PinmemberEddi Rae4:45 11 Jul '11  
AnswerRe: Getting error - 3 is not a valid status code on contact PinmemberEddi Rae3:16 27 Jul '11  
GeneralError using this code Pinmembermichelle sollicito4:47 13 May '11  
Generalonly the system fields are getting mapped. Custom fields are not getting mapped. Pinmemberraghav.mohit1:39 7 Apr '10  
GeneralI tried this example but getting the error... PinmemberIMMujeeb4:00 14 Apr '09  
GeneralRe: I tried this example but getting the error... PinmemberIMMujeeb4:46 14 Apr '09  
QuestionDoesn't show in contacts? Pinmembermgrif5:04 25 Oct '07  
Cem,
 
I tried your code but the contact doesn't show up in contacts after i execute this on a specific leadid. There are no errors, so i'm a missing something?
 
Regards,
 
Maarten Griffioen
AnswerRe: Doesn't show in contacts? PinmemberCem Onvar22:20 11 Nov '07  
AnswerRe: Doesn't show in contacts? Pinmemberberend3:12 13 Aug '09  
AnswerRe: Doesn't show in contacts? PinmemberNathan Greenway16:08 27 Jan '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 15 Jun 2007
Article Copyright 2007 by Cem Onvar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid