Skip to main content
Hi ,

I am trying to create a trigger to update two custom fields on Lead object with two values from a Custom object. The trigger is getting executed but it is not working as expected i.e. Branch Name and Distance to Branch are not getting populated. 

Here is the Trigger code

trigger UpdateBranchAndDistanceToBranch on Lead (before insert) {

Set<Id> lstLeadsToUpdate = new Set<Id>();    

    for( Lead l : trigger.new )

    {  system.debug('l.PostalCode....' + l.PostalCode);

        //Get the list of all leads that need an update

        If(l.PostalCode != null && l.Branch_Name__C == null )

        {    

            lstLeadsToUpdate.add(l.Id);

        }

    }

    if(lstLeadsToUpdate.isEmpty() == false)

    {

     List<Lead> finallstLeadsToUpdate=new List<Lead>();

     List<Lead> listLeads = [SELECT id,Branch_Name__c,Distance_To_Branch__c, PostalCode  from Lead WHERE Id in :lstLeadsToUpdate];

       system.debug('listLeads ...' + listLeads);

     For (Lead oLead : listLeads) 

        {   

         Serviced_Postal_Code__c sp=[select Sub_Branch__c,Distance_From_Branch_mi__c from Serviced_Postal_Code__c where Postal_Code__c= : oLead.PostalCode];

         Lead c=new Lead(Id=oLead.Id);

       

         system.debug('sp.Sub_Branch__c..' + sp.Sub_Branch__c);

         system.debug('sp.Distance_From_Branch_mi__c..' + sp.Distance_From_Branch_mi__c);

       

         c.Branch_Name__c =  sp.Sub_Branch__c;

         c.Distance_To_Branch__c  =  sp.Distance_From_Branch_mi__c; 

         finallstLeadsToUpdate.add(c);   

         }  

      

        if (finallstLeadsToUpdate.size()>0)

           {

                update finallstLeadsToUpdate;

            }  

    }               

}

First debug statement(system.debug('l.PostalCode....' + l.PostalCode);) is displaying the postal code that i am keying in for the lead.

The debug log is showing zero rows for "SELECT id,Branch_Name__c,Distance_To_Branch__c, PostalCode  from Lead WHERE Id in :lstLeadsToUpdate"

I am unable to figure out what am I missing in the code that is causing the issue.

Kindly let me know where am I going wrong.

Thanks

Rao

 
8 answers
  1. Jan 11, 2019, 7:02 PM
    Hi,

      Please run this code. You don't need to take care of update statement. Recordds automatically committed into database.

    trigger UpdateBranchAndDistanceToBranch on Lead (before insert) {

    Set<String> postalCodeList = new Set<String>();

    List<Lead> leadList = new List<Lead>();

    for( Lead l : trigger.new )

    {

    If(l.PostalCode != null && l.Branch_Name__C == null )

    {

    postalCodeList.add(l.PostalCode);

    leadList.add(l);

    }

    }

    Map<String, Serviced_Postal_Code__c> codeBrachMap = new Map<String, Serviced_Postal_Code__c>();

    for(Serviced_Postal_Code__c spc : [SELECT Sub_Branch__c,Postal_Code__c,Distance_From_Branch_mi__c FROM Serviced_Postal_Code__c

    WHERE Postal_Code__c IN :postalCodeList]){

    codeBrachMap.put(spc.Postal_Code__c, spc);

    }

    for(Lead ld : leadList){

    Serviced_Postal_Code__c tempSPC = codeBrachMap.get(ld.PostalCode);

    ld.Branch_Name__c = tempSPC.Sub_Branch__c;

    ld.Distance_To_Branch__c = tempSPC.Distance_From_Branch_mi__c;

    }

    }

    If you have any query you can ping me directly. If you got ypur answer please mark it as best answer so that othes can get help.

    Thanks,

    Banwari

    skype: bkevat92

     
0/9000