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:
- Salesforce.com allows you to automatically map standard and custom lead fields to account, contact, and opportunity fields.
- 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:
- Whenever a new contact is created from a lead, a custom object is created that is associated to the contact.
- Whenever a new account is created, a callout is made to an external webservice.
- 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; } } } } |
Related posts:
- Modifying Salesforce.com System Fields during Data Migration
- Relationship Lookup Objects in Triggers are NULL?
- Automating Salesforce Approval Processes with Apex Triggers
- Salesforce.com Critical Update – Workflow Rule and Roll-Up Summary Field Evaluations Up
- Preventing Recursive Future Method Calls in Salesforce
Categories: Apex, Salesforce




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.
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!!
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
Will, I wrote a Trigger like this for Informa. Something like CustomLeadConversion or something.
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?
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.
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.
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.