0

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.

Code:

 public void beforeInsert(SObject so) {

        Account newAccount = (Account)so;
if(newAccount<>null)
        {
            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)
            { 
            acctMap.put(l.ConvertedAccountId,l.Parent__c); 
            } 
            for (account a:acc)
            { 
            if (acctMap.containsKey(a.Id))
            { 
            a.ParentId=acctMap.get(a.Id); 
            } 
            } 


        }

}

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

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){
                    contactIds.add(l.ConvertedContactId);
                    accountsToCheck.put(l.ConvertedAccountId, new Lead(BDM_MRR__c = l.BDM_MRR__c,LeadSource = l.LeadSource, Description = l.Description));
                    leadAccount.put(l.Id,l.ConvertedAccountId);
                } 
            }
        }
        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);
            MapTraffics.put(GA.Lead__c,TrafficsOfGALead);
        }

        if(accountsToCheck.keySet().size()>0){
            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';                
                recordsToUpdate.add(ev);
            }
            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);
                recordsToUpdate.add(GA);
            }
            if(recordsToUpdate.size()>0){
                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));
            }           
            if(!acmToUpsert.isEmpty()){
                upsert acmToUpsert UniqueKey__c;
            }
        }
    }
}
  • 1
    Thank You so much..I have modified the code and mapping is done to account from lead – Dhananjay Patil Jul 12 '16 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.