Enhancing the Lead Conversion Process in Salesforce.com

February 13th, 2009

During the Salesforce.com lead conversion process, you can create an account, contact and opportunity for the lead that is being converted. The process is pretty straightforward and Salesforce.com provides some tools for customizing it:

  1. Salesforce.com allows you to automatically map standard and custom lead fields to account, contact, and opportunity fields.
  2. Apex triggers are fired and universally required custom fields and validation rules are enforced only if validation and triggers for lead convert are enabled in your organization.

However, there may be some instances when a use case requires more complex processing. For instance:

  1. Whenever a new contact is created from a lead, a custom object is created that is associated to the contact.
  2. Whenever a new account is created, a callout is made to an external webservice.
  3. Whenever a new opportunity is created, a number of standard products are added to the opportunity.

Here is a sample trigger that, for simplicity, does not operate for bulk inserts but gives you a good head start. The documentation on the conversion process is located here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
trigger LeadConvert on Lead (after update) {
 
  // no bulk processing; will only run from the UI
  if (Trigger.new.size() == 1) {
 
    if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {
 
      // if a new account was created
      if (Trigger.new[0].ConvertedAccountId != null) {
 
        // update the converted account with some text from the lead
        Account a = [Select a.Id, a.Description From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
        a.Description = Trigger.new[0].Name;
        update a;
 
      }          
 
      // if a new contact was created
      if (Trigger.new[0].ConvertedContactId != null) {
 
        // update the converted contact with some text from the lead
        Contact c = [Select c.Id, c.Description, c.Name From Contact c Where c.Id = :Trigger.new[0].ConvertedContactId];
        c.Description = Trigger.new[0].Name;
        update c;
 
        // insert a custom object associated with the contact
        MyObject obj = new MyObject();
        obj.Name = c.Name;
        obj.contact__c = Trigger.new[0].ConvertedContactId;
        insert obj;
 
      }
 
      // if a new opportunity was created
      if (Trigger.new[0].ConvertedOpportunityId != null) {
 
        // update the converted opportunity with some text from the lead
        Opportunity opp = [Select o.Id, o.Description from Opportunity o Where o.Id = :Trigger.new[0].ConvertedOpportunityId];
        opp.Description = Trigger.new[0].Name;
        update opp;
 
        // add an opportunity line item
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.Quantity = 1;
        oli.TotalPrice = 100.00;
        oli.PricebookEntryId = [Select p.Id From PricebookEntry p Where CurrencyIsoCode = 'USD' And IsActive = true limit 1].Id;
        insert oli;
 
      }         
 
    }
 
  }     
 
}
VN:F [1.9.22_1171]
Rating: 8.2/10 (6 votes cast)
VN:F [1.9.22_1171]
Rating: +5 (from 5 votes)
Enhancing the Lead Conversion Process in Salesforce.com, 8.2 out of 10 based on 6 ratings

Categories: Apex, Code Sample, Salesforce

Leave a comment

Comments Feed18 Comments

  1. Matt

    Hi Jeff, as a Salesforce.com consulting partner, something I get asked all the time is:

    ‘Can a custom object record related to a Lead be carried over to the newly created Contact upon conversion?’

    Would the code you provided above do that?
    If not, how would one go about achieving this?

    Key thing to remember here is that the custom object record already exists.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  2. jeffdonthemic

    Matt, we currently have this same kind of functionality for one of our companies during the lead conversion process. So essentially our custom object has a lead__c and contact__c lookup field. So during the conversion process the trigger above would convert the lead to a contact, query the custom objects for a record that matches the lead and then, if found, updates that custom object with the newly created contact id.

    Hope this helps!!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
  3. Will Donovan

    Hi Jeff and Matt – Jeff, did you end up creating a Trigger that accomplished what you were looking for? Our organization is looking for exactly that – about to farm it out out to ODesk, but if you were able to come up with it, and are willing to share…

    Anyways, feel free to e-mail me directly at wdonovan@gmail.com

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  4. jeffdonthemic

    Will, I wrote a Trigger like this for Informa. Something like CustomLeadConversion or something.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  5. Steven

    Jeff, Would you mind sharing the trigger code? It would be a huge help for us.

    Also, do you know if it is possible to customize the Lead Convert page to include a Description field that maps to the newly created opportunity?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  6. jeffdonthemic

    Steven, would not mind at all. The code is right there on the post so feel free to copy it. Unfortunately I don’t think you are going to be able to customize the Lead Convert page to include the description for mapping.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  7. Sree

    Hi Jeff,

    Do you know if there is a way to add a validation when user click on Convert Lead button and then stop them from converting the lead if validation fails.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  8. Jeff Douglas

    That functionality isn’t possible with the standard Salesforce.com UI. You’ll have to write your own lead convert Visualforce/Apex to do this.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  9. Motely

    Jeff,

    I may be missing something, but in response to Matt, you said that you developed code to query the custom objects for objects associated with the lead. I am trying to do this, but for somereason in my query it won’t let me use lead.id for the lead just converted. Is it now called something else since it was converted?

    Am I making sense?

    David

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  10. Warren

    Hi Jeff,

    Thank you so much for the post, it has saved me a lot of brain conundrums :)

    Regards,
    Warren

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  11. Brandi

    Can you post an example of how the test class might look? I’ve done something simple in my org and can’t get the test class to work properly :(

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  12. Mike

    Hi Jeff,

    I’ve been scouring the internet for something to accomplish what I believe your code above does.

    Currently, I have a custom lead field called lead_firm_c that IDs the lead’s Account association. This is so we can view all our leads and contacts on the Account page. But upon conversion of a lead this field is lost.

    I’ve never made a VF page or have done any coding for salesforce. But any help regarding where I should be looking to get this accomplished or what needs to be changed in the above code to do this. Anything at all would be greatly appreciated.

    Thanks!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  13. Jeff Douglas

    @Mike, take a look at that trigger. You should just be able to modify the code to find right account and update the ID from the lead. Sorry I couldn’t be more specific.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  14. Jasper

    Hi Jeff,
    your article conviced me that there is a solution for the issue I’m working on. I got pretty handy with “finetuning” our SFDC environment but I’m completely new in Apex.

    What I want to establish is the following: Using web-to-lead on an external website I manage to get (besides contact and account details) simple orders in SFDC as a lead. So what I want next is that if this particulair custom lead fields containing order details are filled, a “contract” on the account is created when the lead is converted. Where the values from the custom lead fields are transponed to the similair custom contract fields.

    I’ve being staring to the “create new apex trigger” screen in our sandbox environment for some time now but don’t have a clue how to start..can you help me?

    Much appreciated!

    regards,

    Jasper

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  15. Jeff Douglas

    @Jasper, sounds like you need to pick of a copy of our Salesfore Handbook as this is covered in depth.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  16. Brian Gontarski

    Jeff,

    I am struggling to map Lead Attachment​s to an Opportunit​y upon Conversion (the way Activity History maps to both contacts and opportunities when converting.

    I’ll buy 2 handbooks if you can help me determine how this simple request can be done :)

    Brian G.

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.22_1171]
    Rating: +1 (from 1 vote)
  17. sreenath

    Hi Jeff,
    Error: Compile Error: Invalid type: MyObject at line 27 column 28. while i am using your trigger code for lead conversion process in my developer account. Any suggestions plzzz..

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  18. Steve

    Hi Jeff,

    Have you run nto the issue that the LEAD object is not a relate-able item in the send email or in the use of visualforce email templates. The lead ID is required during test of the visualforce template.

    So lets sale you have a lead and you want to sen an email to the lead with a VF email template. It will not work!

    Seems very strange…. On all other standard objects like opportunity when you click on the sen email button you get a relatedto dropdown listing all of the objects. Leads are missing!

    Any ideas?

    Thanks

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)

Leave a comment

Feed

http://blog.jeffdouglas.com / Enhancing the Lead Conversion Process in Salesforce.com