function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jsacpt24jsacpt24 

Apex Trigger after lead is converted

I am trying to write a trigger that updates Account, Contact and Opportunity with the lead after that said lead is converted. Does anyone have the most recommended practice for this? 
Amit Chaudhary 8Amit Chaudhary 8
You can create the trigger on Lead like below
trigger test on Lead (before update) {
 for(Lead lead:System.Trigger.new) {
  if (Lead.IsConverted)
  //do somethign here with converted leads
 }
}
Please check below post for ssame
1) http://blog.jeffdouglas.com/2009/02/13/enhancing-the-lead-convert-process-in-salesforce/
2) http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
3) http://eltoro.force.com/TriggerForLeadConversion
4) http://www.bulkified.com/How+to+Customize+the+Lead+Conversion+Process+in+Salesforce.com
trigger lead_onConvert on Lead (after update) {

	/*
	 * Create variables to store the lead values,
	 * new and old.  This will be used below to
	 * determine if the leads are being converted,
	 * not converted yet, or has already been
	 * converted.
	 */
	List<Lead> newLeads = trigger.new;
	Map<Id, Lead> mOldLeads = trigger.oldMap;
	Lead oldLead;
	
	/*
	 * Create sets of Ids to store the records
	 * associated with the converted leads
	 */
	Set<Id> convertedAccountIds = new Set<Id>();
	Set<Id> convertedContactIds = new Set<Id>();
	Set<Id> convertedOpportunityIds = new Set<Id>();

	/*
	 * Loop through the leads submitted through this
	 * trigger.  Populate the appropriate sets of Ids
	 * for each lead with populated values.
	 */
	for (Lead l : newLeads) {
		
		if (l.convertedAccountId != null) {
			convertedAccountIds.add(l.convertedAccountId);
		}
		
		if (l.convertedContactId != null) {
			convertedContactIds.add(l.convertedContactId);
		}
		
		if (l.convertedOpportunityId != null) {
			convertedOpportunityIds.add(l.convertedOpportunityId);
		}
	}
	
	/*
	 * Extract the records associated with each set of
	 * Ids populated above.  Once the data has been extracted
	 * store it in a map so it can efficiently be referenced
	 * in the main loop below.
	 */
	List<Account> accounts =
		[SELECT Id, Name
		 FROM Account
		 WHERE Id IN : convertedAccountIds];
		 
	Map<Id, Account> mAccounts = new Map<Id, Account>(accounts);
	
	List<Contact> contacts =
		[SELECT Id, Name
		 FROM Contact
		 WHERE Id IN : convertedContactIds];
		 
	Map<Id, Contact> mContacts = new Map<Id, Contact>(contacts);
	
	List<Opportunity> opportunities =
		[SELECT Id, Name
		 FROM Opportunity
		 WHERE Id IN : convertedOpportunityIds];
		 
	Map<Id, Opportunity> mOpportunities = new Map<Id, Opportunity>(opportunities);

	/*
	 * Create lists of records to be updated in bulk at
	 * the end of this trigger.  We use a separate list
	 * to ensure we aren't updating records that are
	 * not affected by this trigger.
	 */
	List<Account> updateAccounts = new List<Account>();
	List<Contact> updateContacts = new List<Contact>();
	List<Opportunity> updateOpportunities = new List<Opportunity>();

	/*
	 * MAIN LOOP
	 *
	 * Loop through all leads submitted through this trigger.
	 * If this trigger was executed because of a conversion
	 * we will populate the related records.
	 */
	for (Lead newLead : newLeads) {
		
		// Get a reference to the old values associated
		// with this lead.
		oldLead = mOldLeads.get(newLead.Id);
		
		// Determine if the lead was converted or not.
		// If the previous status was not converted and
		// the current status is converted, then this
		// trigger executed because of a conversion.
		if (!oldLead.IsConverted && newLead.isConverted) {
		
			// ** CONVERSION FOUND **
			
			Account account = mAccounts.get(newLead.convertedAccountId);
			// Make appropriate updates here.
			updateAccounts.add(account);
			
			Contact contact = mContacts.get(newLead.convertedContactId);
			// Make appropriate updates here.
			updateContacts.add(contact);
			
			Opportunity opportunity = mOpportunities.get(newLead.convertedOpportunityId);
			// Make appropriate updates here.
			updateOpportunities.add(opportunity);
			
		}
	}
	
	/*
	 * Update the records for any update list
	 * populated with records.
	 */ 
	if (updateAccounts.size() > 0) {
		update updateAccounts;
	}
	
	if (updateContacts.size() > 0) {
		update updateContacts;
	}
	
	if (updateOpportunities.size() > 0) {
		update updateOpportunities;
	}
}


Let us know if this will help you
 
jsacpt24jsacpt24
Originally I have this bit of code but it didn't seem to be working. I keep getting "Error: Compile Error: line 2:0 no viable alternative at character ' ' at line 2 column 0". Currently we already have a convert button that is working. I wanted to try and have it where upon creation it put the ID of the lead in the field that we created (i.e. Opportunity.Lead__c). Is that something that you would not recommend? 
trigger test on Lead (before update) {
 for(Lead lead:System.Trigger.new) {
  if (Lead.IsConverted){
    Opportunity.Lead__c = Lead.id;
    Account.Lead__c = Lead.id;
    Contact.Lead__c = Lead.id;
}
 }
}

 
jsacpt24jsacpt24
Currently the VF that we have working as our Convert is 
<!-- Use this page to override the standard Lead Convert action. This page will
     check a checkbox field on the Lead record; checking that checkbox will
     cause the Lead to not meet the workflow rule's criteria, which will remove
     the Lead from the workflow queue, and allow it to be converted. -->
<apex:page standardController="Lead" > 

<apex:form >
    <div style="visibility:hidden;">
        <apex:inputField value="{!Lead.Cancel_Workflow__c}" id="cancelWorkflow" style="visibility:hidden; "/>
    </div>

<apex:actionFunction name="quickSave" action="{!quickSave}" oncomplete="standardConvert();"/>
    <apex:actionFunction name="standardConvert"
      action="{!URLFOR($Action.Lead.Convert, lead.id, [retURL=$CurrentPage.parameters.retURL], true)}" />

<script language="JavaScript">

    // When the page finishes loading, do the default window.onload action,
    // then call our fixLead() function.
    var previousOnload = window.onload;
    window.onload = function() {
        if (previousOnload) previousOnload();
        fixLead();
    }

    // Edit the Lead to set the Cancel Workflow flag.
    // When quickSave() finishes, it will redirect to the default Convert action.
    function fixLead() {
        var elemCancelWorkflow = document.getElementById('{!$Component.cancelWorkflow}');
        elemCancelWorkflow.checked = true;
        quickSave();
    }

</script>
</apex:form>
</apex:page>