CRMBuzz.net

Abe Saldana on Microsoft Dynamics CRM Development
posts - 23, comments - 0, trackbacks - 0

Converting a Lead to Contact and Account

Working on a special project, I have to recreate the Lead conversion and make some other customizations on the Accounts and Contact relationship, the first code I was looking was the conversion using the Relationship mapping, I found some code regarding the related request/response, but looking under the SDK documentation finally find some code hints that I can use for the conversion.

The following is the code for converting a source entity to a target entity, taking in consideration the relationship mapping using the TargetRelatedRequest and response classes (CRM 3.0 SDK)

 

/// <summary>
///  Convert to Contact, Convert/Create first the Contact then the Account
/// </summary>
public Guid ConvertLead2Contact(Guid leadIdvalue)
{
InitializeFromRequest requestObject = new InitializeFromRequest();
 
requestObject.EntityMoniker = new Moniker();
requestObject.EntityMoniker.Id = leadIdvalue;
requestObject.EntityMoniker.Name = EntityName.lead.ToString();
 
requestObject.TargetEntityName = EntityName.contact.ToString();
requestObject.TargetFieldType = TargetFieldType.All;
 
InitializeFromResponse responseObject = (InitializeFromResponse)service.Execute(requestObject);
 
contact returnContact = (contact) responseObject.Entity;
 
    // You need to create the new created Contact Entity
    Guid returnContactId = service.Create(returnContact.Entity);
 
return returnContactId;
}

 

/// <summary>
/// Convert Lead to Account, Convert the Account after the Contact.
/// </summary>
public Guid ConvertLead2Account(Guid leadIdvalue, Guid primaryContactId)
{
InitializeFromRequest requestObject = new InitializeFromRequest();
 
      requestObject.EntityMoniker = new Moniker();
      requestObject.EntityMoniker.Id = leadIdvalue;
      requestObject.EntityMoniker.Name = EntityName.lead.ToString();
 
      requestObject.TargetEntityName = EntityName.account.ToString();
      requestObject.TargetFieldType = TargetFieldType.All;
 
InitializeFromResponse responseObject = 
    (InitializeFromResponse)service.Execute(requestObject);
 
      account returnAccount = (account) responseObject.Entity;
 
if(primaryContactId != null)
{
 
    If(primaryContactId != Guid.Empty)
    {
// relationship with the Contact
returnAccount.primarycontactid = 
CrmTypes.CreateLookup(EntityName.contact.ToString(),primaryContactId);
 
    }
 
}
 
    // You need to create the new converted Account
    Guid returnAccountId = service.Create(returnAccount.Entity);
 
      return returnAccountId;
 
}

Notes:

I assume you already created or added the CRM Web Service reference and created the CrmService object (service) in your project. Also I'm using the CrmTypes class to help create CRM type objects

The Lead status is not change using the TargetRelatedRequest I will post another article where is going to guide you and have code snippets to check the value, Qualify, Disqualify and Reactivate the Lead.

Hope this article will help.

Abraham Saldaña

http://crmbuzz.net

Print | posted on Wednesday, May 28, 2008 4:36 AM | Filed Under [ Callouts Code Level 300 MS Dynamics CRM 3.0 SDK code Unsupported ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 7 and type the answer here:

Powered by: