I have a requirement inwhich I have to create a custom lookup field on lead object which selects the account and during the lead to account conversion I have to map the field with 'Parent Account' field of the account which is a standard field.with the help of Standard Mapping it is not possible.So i am trying to write the code on before insert trigger that maps the custom lookup field of lead to the standard field of account.


 public void beforeInsert(SObject so) {

        Account newAccount = (Account)so;
            List<Lead> convertedLeads=[SELECT Id, ConvertedAccountID, Parent__c FROM Lead WHERE IsConverted=True AND ConvertedAccountId <>NULL]; 
            Map<ID,ID> acctMap=new Map<ID,ID>(); 
            List<Account> acc=[Select Id,ParentId from Account where Id=:newAccount.Id Limit 1];
            for (lead l: convertedLeads)
            for (account a:acc)
            if (acctMap.containsKey(a.Id))



but unfortunately it does not map the lead field with account.Can you please suggest what i am missing here.

1 Answer 1


I suggest you to do a after update on Lead Trigger. This is due to order of execution on Lead conversion. You can find some information here : Lead Conversion Trigger Order of Execution

Then you have to check isConverted has changed. There you can take the field in trigger.new and move it to the account by using ConvertedAccountId. I paste some code I used to do something similar.

In this example you can see I map Leadsource Into accountSource, Description && other fields among creating other objects.

public without sharing class TriggerLeadConversionEvents implements Triggers.Handler{

    Set<Id> contactIds = new Set<Id>();
    Map<Id,List <Ga_Traffic__c>> MapTraffics = new Map<Id,List <Ga_Traffic__c>> ();
    Map<Id,Id> leadAccount = new Map<Id,Id>();
    Map<Id,Lead> accountsToCheck = new Map<Id,Lead>();

    public void handle(){

        Map<Id,Lead> oldMap = (Map<Id,Lead>) trigger.oldMap;    
        for (Lead l : (List<Lead>) trigger.new){
            if(l.isConverted != oldMap.get(l.id).isConverted){
                if(l.convertedAccountId != null){
                    accountsToCheck.put(l.ConvertedAccountId, new Lead(BDM_MRR__c = l.BDM_MRR__c,LeadSource = l.LeadSource, Description = l.Description));
        for(Ga_Traffic__c GA:[SELECT id,Lead__c,Account__c FROM Ga_Traffic__c WHERE Lead__c in: leadAccount.keySet()]) {
            List <Ga_Traffic__c> TrafficsOfGALead = MapTraffics.get(GA.lead__c);
            if(TrafficsOfGALead==NULL) TrafficsOfGALead = new List <Ga_Traffic__c> {GA};
            else  TrafficsOfGALead.add(GA);

            List<sObject> recordsToUpdate = new List<sObject>();     
            for (Account acc : [SELECT Id, AccountSource FROM Account WHERE Id IN :accountsToCheck.keySet()]){
                if(accountsToCheck.get(acc.Id)!=null && (acc.AccountSource == null || acc.AccountSource == '')){
                    recordsToUpdate.add(new Account(BDM_MRR__c = accountsToCheck.get(acc.id).BDM_MRR__c,Id=acc.Id, AccountSource=accountsToCheck.get(acc.Id).LeadSource, Description =accountsToCheck.get(acc.Id).Description));

            for(Event ev : [SELECT Id, Demo_Status__c 
                            FROM Event 
                            WHERE Demo_status__c='Scheduled' 
                            AND WhoId IN :contactIds 
                            AND Type IN ('Web Demo','Meeting + Demo')]){
                ev.Demo_Status__c = 'Completed';                
            for(Ga_Traffic__c GA: [SELECT Lead__c,Account__c FROM Ga_Traffic__c WHERE Lead__c in: leadAccount.keySet()]) {
                GA.Account__c = leadAccount.get(GA.lead__c);
                update recordsToUpdate;

            List<AccountCampaignMember__c> acmToUpsert = new List<AccountCampaignMember__c>();
            for(CampaignMember cm : [SELECT LeadId, CampaignId FROM CampaignMember WHERE LeadId IN :leadAccount.keySet()]){
                acmToUpsert.add(new AccountCampaignMember__c(CampaignRef__c = cm.CampaignId, AccountRef__c = leadAccount.get(cm.LeadId), UniqueKey__c = String.valueOf(leadAccount.get(cm.LeadId)) + cm.CampaignId));
                upsert acmToUpsert UniqueKey__c;
  • 1
    Thank You so much..I have modified the code and mapping is done to account from lead
    – SFDCDJ
    Jul 12, 2016 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .