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
CRMsantabarbaraCRMsantabarbara 

Trigger only fires on first record of batch update/insert action via dataloader

Hi,  my trigger only fires for the first record in the batch when performing a dataloader insert.  Any ideas on how to get it to fire for all the records in each batch?  Dataloader is set to batchs of 200.  I don't want to reduce the batch to a count of 1, I need it to do bulk.  The trigger is designed to perform a vlookup (using the zip code on the lead as a reference) on a custom object (Postal Code Test)  and populate a custom field on the Lead object.  Each record on the Postal Code Test object contains a zip code and a corresponding internal territory.

 

here's the trigger:

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

    try {

        for(Lead x : Trigger.New) {

            //set to no area until it finds a territory matching the zip

            x.Lead_Territory_Trigger__c = 'NOAREA';

           

            //fetch territory from custom object using postal code from lead

            List<Postal_Code_Test__c> triggerLeadArea =

                [SELECT p.Lead_Territory__c

                FROM Postal_Code_Test__c p

                WHERE Zip_Code__c =: x.PostalCode

                LIMIT 1];

           

            //update lead territory field on lead

            x.Lead_Territory_Trigger__c = triggerLeadArea[0].Lead_Territory__c;       

        }

    } 

    catch (Exception e) {      

        System.Debug('ERROR: ' + e);

    }

}

Best Answer chosen by Admin (Salesforce Developers) 
pankaj.raijadepankaj.raijade

Hey Bob is correct; find correction below in red.

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c where Zip_Code__c in :leadListWithPostal];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

   Integer i = 0;

        for(Postal_Code_Test__c oPostalCodeTest : lstPostalCodetest){

           leadPostalMap.put(lstPostalCodeTest.get(i).Zip_Code__c, lstPostalCodeTest.get(i).Lead_Territory__c);

           i ++;

        }

    

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.PostalCode)​;

   }

All Answers

Rocky.4QRocky.4Q

Depends on how many records you have. If you're loading less than 200 records it's only going to call that trigger once and pass all the records in the trigger.new list that you're itterating through.

bob_buzzardbob_buzzard

Do you see the output from the exception handler in the debug log?  

pankaj.raijadepankaj.raijade

There is one major problem in this code.

 

There is a Query inside loop. This will fail in you insert or update more than 100 records.

You will have to bulkify this trigger and try it if it works?

 

loop through lead make a set of PostalCode

retrive all Postal_Code_Test__c in one query.

loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>

 

now again loop through leads and update Lead_Territory_Trigger__c

 

Regards,

Pankaj Raijade,

CRMsantabarbaraCRMsantabarbara

Thanks for your reply. I was able to bulkify the Trigger and got it to compile successfully but now when trying to insert a new record I get the following error message:

 

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger UpdateLeadAreaAssign caused an unexpected exception, contact your administrator: UpdateLeadAreaAssign: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateLeadAreaAssign: line 20, column 1

 

I have tried to update the x.Id to x.PostalCode but get the same error. I apologize for the simple questions. Below is the new trigger code.

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

   

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> postalList = [select Zip_Code__c,

                           Lead_Territory__c from Postal_Code_Test__c];

 

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>

   Map<String,Postal_Code_Test__c> leadPostalMap = new Map<String,Postal_Code_Test__c>

        ([SELECT Lead_Territory__c from Postal_Code_Test__c

        where Zip_Code__c in :leadListWithPostal]);

       

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.Id).Lead_Territory__c;

   }

}

VarunSforceVarunSforce

select Id too, during building leadPostalMap , as following and try

 

Map<String,Postal_Code_Test__c> leadPostalMap = new Map<String,Postal_Code_Test__c>

        ([SELECT Id, Lead_Territory__c from Postal_Code_Test__c

        where Zip_Code__c in :leadListWithPostal]);



CRMsantabarbaraCRMsantabarbara

Thank you for the help but it doesn't work.  I have tried that and also adding in the Zip_Code__c since that is Id field relating the two objects but still get the same error.

VarunSforceVarunSforce

What i understood is - you want to fetch lead Territory based on lead zip code, try following,

 

       Set<String> leadListWithPostal  = new Set<String>(); 
       for(Lead x : Trigger.new)
       {
           leadListWithPostal.add(x.PostalCode);
       }

           Map<String,Postal_Code_Test__c> leadPostalMap = new Map<String,Postal_Code_Test__c>
        ([SELECT Zip_Code__c,Lead_Territory__c from Postal_Code_Test__c where Zip_Code__c in   :leadListWithPostal]);


       for(Lead x:  Trigger.new)
       {
            x.Lead_Territory_Trigger__c = leadPostalMap.get(x.Zip_Code__c).Lead_Territory__c;
       }

CRMsantabarbaraCRMsantabarbara

my map "leadPostalMap" is returning the Id of the custom, unrelated object Postal_Code_Test__c. I have tried to make the map list Lead.Id and Postal_Code_Test__c.Lead_Territory__c but it is listing all columns from Postal_Code_Test__c. Why?"

CRMsantabarbaraCRMsantabarbara

Zip_Code__c is on my custom object Postal_Code_Test__c

not on the standard Lead object

pankaj.raijadepankaj.raijade

The code should be like this:

 

 Set<String> leadListWithPostal  = new Set<String>(); 
       for(Lead x : Trigger.new)
       {
           leadListWithPostal.add(x.PostalCode);
       }

list<Postal_Code_Test__c> lstPostalCodetest = [SELECT Zip_Code__c,Lead_Territory__c from Postal_Code_Test__c where Zip_Code__c in   :leadListWithPostal];

 

        Map<String,string> leadPostalMap = new Map<String,string>();

for(Postal_Code_Test__c oPostalCodetest : lstPostalCodetest)
leadPostalMap.put(Zip_Code__c, Lead_Territory__c );

       for(Lead x:  Trigger.new)
       {
            x.Lead_Territory_Trigger__c = leadPostalMap.get(x.Zip_Code__c).Lead_Territory__c;
       }

 

 

This should work.

 

Regards,

Pankaj Raijade

CRMsantabarbaraCRMsantabarbara

 It looks good. But got an error “Error: Compile Error: Variable does not exist: Zip_Code__c at line 16 column 26

 

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

     for(Postal_Code_Test__c oPostalCodetest : lstPostalCodetest)

       leadPostalMap.put(Zip_Code__c, Lead_Territory__c );

    

     

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.Zip_Code__c).Lead_Territory__c​;

       

   }

}

pankaj.raijadepankaj.raijade

try this out:

 In query I added where clause , this will get only required records and avoid unnecessert processing.

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c where Zip_Code__c in :leadPostalMap];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

     for(Postal_Code_Test__c oPostalCodetest : lstPostalCodetest)

       leadPostalMap.put(oPostalCodetest.Zip_Code__c, oPostalCodetest .Lead_Territory__c );

    

     

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.Zip_Code__c).Lead_Territory__c​;

       

   }

CRMsantabarbaraCRMsantabarbara

Tried what you asked but got the below error message:

 

Error: Compile Error: Variable does not exist: Zip_Code__c at line 16 column 26

 

With the following code:

 

“trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c where Zip_Code__c in :leadListWithPostal];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

     for(Postal_Code_Test__c oPostalCodetest : lstPostalCodetest){

       leadPostalMap.put(Zip_Code__c, Lead_Territory__c );

     }

    

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.PostalCode).Lead_Territory__c​;

        //system.debug('lstPostalCodeTest: ' + lstPostalCodeTest.get(0));

   }

}”

 

Tried changing to Zip_Code__c and Lead_Territory__c but got compilation errors:

 

When changing to oPostalCodetest.Zip_Code__c and oPostalCodetest.Lead_Territory__c then get:

Error: Compile Error: Initial term of field expression must be a concrete SObject: String at line 21 column 71

 

When changing to lstPostalCodetest.Zip_Code__c and lstPostalCodetest.Lead_Territory__c then get:

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Postal_Code_Test__c> at line 16 column 26

 

Thanks for your help.

CRMsantabarbaraCRMsantabarbara

I figured out how to get the mapping to work now I am having trouble updating the field on the Lead. I am getting the below error:

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: String at line 24 column 71

 

The updated code is below:

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c where Zip_Code__c in :leadListWithPostal];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

   Integer i = 0;

        for(Postal_Code_Test__c oPostalCodeTest : lstPostalCodetest){

           leadPostalMap.put(lstPostalCodeTest.get(i).Zip_Code__c, lstPostalCodeTest.get(i).Lead_Territory__c);

           i ++;

        }

    

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.PostalCode).Lead_Territory__c​;

   }

}

bob_buzzardbob_buzzard

In this line:

 

   x.Lead_Territory_Trigger__c = leadPostalMap.get(x.PostalCode).Lead_Territory__c​;

 

you are assuming there will be an sobject returned from the map, but leadPostalMap has strings for both the key and value.

pankaj.raijadepankaj.raijade

Hey Bob is correct; find correction below in red.

 

trigger UpdateLeadAreaAssign on Lead (before Insert, before Update) {

 

   //loop through lead make a set of PostalCode

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

   for(Lead x : Trigger.new) {

       leadListWithPostal.add(x.PostalCode);

   }

        

   //Retrieve all Postal_Code_Test__c in one query

   List<Postal_Code_Test__c> lstPostalCodeTest = [select Zip_Code__c, Lead_Territory__c

                                                   from Postal_Code_Test__c where Zip_Code__c in :leadListWithPostal];

  

   //Loop through Postal_Code_Test__c create map for <postalcode, Postal_Code_Test__c>  

   Map<String,string> leadPostalMap = new Map<String,string>();

   Integer i = 0;

        for(Postal_Code_Test__c oPostalCodeTest : lstPostalCodetest){

           leadPostalMap.put(lstPostalCodeTest.get(i).Zip_Code__c, lstPostalCodeTest.get(i).Lead_Territory__c);

           i ++;

        }

    

   //now again loop through leads and update Lead_Territory_Trigger__c

   for(Lead x:  Trigger.new){       

        x.Lead_Territory_Trigger__c = leadPostalMap.get(x.PostalCode)​;

   }

This was selected as the best answer
CRMsantabarbaraCRMsantabarbara

Thanks for all your help !