+ Start a Discussion
Surya.236Surya.236 

How to convert the following nested for loop to map

public void UpdateFieldsOnChildrenLeads(Map<Id,Lead> leadMap) {
    
        Map<ID,Lead> childrenLeads = new Map<ID,Lead>();
        Map <ID,List<Lead>> childrenPerLead = new Map <ID, List <Lead>> ();
        Set <String> updatedLeads = new Set <String> ();

       
        for(Lead thisLead:leadMap.values()){
            updatedLeads.add(thisLead.Id);
        }
        
        if(updatedLeads.size()>0)
        childrenLeads = new Map<ID,Lead> ([Select a.Id,
        a.Parent_Lead__c, 
        a.Dosages_Offered__c
         From Lead a
        where a.Parent_Lead__c in :updatedLeads]);
        
         if(childrenLeads.size()>0)
        for (Lead childLead : childrenLeads.values()){
           
            List <Lead> theList = childrenPerLead.get(childLead.Parent_Lead__c);
           
            
            if(theList != null){
                theList.add(childLead); 
            }
          
            else{
                List <Lead> newList = new List <Lead> ();
                newList.add(childLead);
                childrenPerLead.put(childLead.Parent_Lead__c,newList);
            } 
        }
        List <Lead> LeadsToUpdate = new List < Lead>();

        for(Lead thisLead:leadMap.values()){
            List <Lead> lineitems = childrenPerLead.get(thisLead.id);
            if(lineitems != null)
            {
              
                for(Lead li : lineitems)
                {
                    li.State = thisLead.State;
                    li.PostalCode = thisLead.PostalCode;
                 }
                 LeadsToUpdate.addAll(lineitems); 
            }
        }
            if( !LeadsToUpdate.isEmpty() ) 
            update LeadsToUpdate;
    }
Best Answer chosen by Surya.236
Maharajan CMaharajan C
HI Surya,

Try like below:

public void UpdateFieldsOnChildrenLeads(Map<Id,Lead> leadMap) {
    
        Map<ID,Lead> childrenLeads = new Map<ID,Lead>();
        //Map <ID,List<Lead>> childrenPerLead = new Map <ID, List <Lead>> ();
        List<Lead> childLeads = new List<Lead>();
        
        Set <String> updatedLeads = new Set <String> ();

       
        for(Lead thisLead:leadMap.values()){
            updatedLeads.add(thisLead.Id);
        }
        
        if(updatedLeads.size()>0)
        childrenLeads = new Map<ID,Lead> ([Select Id,Parent_Lead__c, Dosages_Offered__c From Lead  where Parent_Lead__c in :updatedLeads]);
        
        if(childrenLeads.size()>0)
        for (Lead childLead : childrenLeads.values()){
            childLeads.add(childLead);  
        }
        List <Lead> LeadsToUpdate = new List < Lead>();

        for(Lead ld:childLeads){
                ld.State = leadMap.get(ld.Parent_Lead__c).State;
                ld.PostalCode = leadMap.get(ld.Parent_Lead__c).PostalCode;
                LeadsToUpdate.add(ld); 
            
        }
            if( !LeadsToUpdate.isEmpty() ) 
            update LeadsToUpdate;
    }

Thanks,
Maharajan.C

All Answers

Surya.236Surya.236
Trigger.newmap is the parameter value for the method
 
Maharajan CMaharajan C
HI Surya,

Try like below:

public void UpdateFieldsOnChildrenLeads(Map<Id,Lead> leadMap) {
    
        Map<ID,Lead> childrenLeads = new Map<ID,Lead>();
        //Map <ID,List<Lead>> childrenPerLead = new Map <ID, List <Lead>> ();
        List<Lead> childLeads = new List<Lead>();
        
        Set <String> updatedLeads = new Set <String> ();

       
        for(Lead thisLead:leadMap.values()){
            updatedLeads.add(thisLead.Id);
        }
        
        if(updatedLeads.size()>0)
        childrenLeads = new Map<ID,Lead> ([Select Id,Parent_Lead__c, Dosages_Offered__c From Lead  where Parent_Lead__c in :updatedLeads]);
        
        if(childrenLeads.size()>0)
        for (Lead childLead : childrenLeads.values()){
            childLeads.add(childLead);  
        }
        List <Lead> LeadsToUpdate = new List < Lead>();

        for(Lead ld:childLeads){
                ld.State = leadMap.get(ld.Parent_Lead__c).State;
                ld.PostalCode = leadMap.get(ld.Parent_Lead__c).PostalCode;
                LeadsToUpdate.add(ld); 
            
        }
            if( !LeadsToUpdate.isEmpty() ) 
            update LeadsToUpdate;
    }

Thanks,
Maharajan.C
This was selected as the best answer