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
SkottypSkottyp 

Trigger won't fire with DataLoader

Hey Team,

My trigger works correctly when changing one record but when I use the data loader it is only assigning 1 sales rep for all the records. I have seen others that have had this same issue and it appears to be something with the mapping, but after a long time of comparing answers in other posts I'm not seeing my error. Please let me know where I am going wrong.
 
trigger AssignDistSales1 on Distributor_Sales__c ( before insert, before update) 
{
    //get set of 3 digit zips from Distributor Sales to be inserted or updated
    Set<String> ZIP = new Set<String>();
    for(Distributor_Sales__c distsales : Trigger.new){
        if(distsales.X3_Digit_Zip__c != null){
            ZIP.add(distsales.X3_Digit_Zip__c);
        }
    }
    
    //list of territories needed to map to Distributor Sales
    List<Territory__c> potentialTerr =  [SELECT X3_Digit_Zip__c, Sales_Rep_for_Later_Assignment__c FROM Territory__c WHERE X3_Digit_Zip__c in :ZIP];
    
    //map that lets me search for territories by their zip
    Map<String,Territory__c> DistToTerrMap = new Map<String, Territory__c>();
        for (Territory__c t: potentialTerr) {
        DistToTerrMap.put(t.X3_Digit_Zip__c, t);
        
    //match the distributor sales 3 digit zip territory by 3 digit zip
    for(Distributor_Sales__c distsales : Trigger.new){
        distsales.Sales_Rep_for_Later_Assignment__c = DistToTerrMap.get(t.X3_Digit_Zip__c).Sales_Rep_for_Later_Assignment__c;
      }
   }
}

 
Best Answer chosen by Skottyp
SkottypSkottyp
Answering my own question here: 
trigger AssignDistSales2 on Distributor_Sales__c (before insert, before update) 
{
    //get set of 3 digit zips from Distributor Sales to be inserted or updated
    List<String> DSzip = new List<String>();
        for(Distributor_Sales__c ds : Trigger.new)
        {
            if(ds.ZipCode__c.length() != 1)
            {
                DSzip.add(ds.X3_Digit_Zip__c);
                //system.debug('5 digits - ' + ds.X3_Digit_Zip__c);
            }      	
        }
    
    //list of territories needed to map to Distributor Sales zip
    List<Territory__c> terr = [SELECT X3_Digit_Zip__c, Sales_Rep__c FROM Territory__c WHERE X3_Digit_Zip__c in :DSzip];
    List<Territory__c> terr2 =  ([SELECT X3_Digit_Zip__c, Sales_Rep_for_Later_Assignment__c FROM Territory__c WHERE X3_Digit_Zip__c in :DSzip]);
    
    //mapping the territory info for Sales Rep
    Map <String, Id> terrMap = new Map<String, Id>();
    for(Territory__c t : terr)
    {
        terrMap.put(t.X3_Digit_Zip__c, t.Sales_Rep__c);
    }
    
    //mapping the territory info for Sales Rep for later assignment
    Map <String, Id> terrMap2 = new Map<String, Id>();
    for(Territory__c t2 : terr2)
    {
        terrMap2.put(t2.X3_Digit_Zip__c, t2.Sales_Rep_for_Later_Assignment__c);
    }
   
   //loop through and assign Sales Rep
   for(Distributor_Sales__c dsnew : trigger.new)
   {
       if(dsnew.X3_Digit_Zip__c !=null)
       {
           dsnew.Sales_Rep__c  = terrMap.get(dsnew.X3_Digit_Zip__c);
    	}   
   			//system.debug('get info - ' +terrMap.get(dsnew.X3_Digit_Zip__c));
        if(dsnew.Sales_Rep__c == null)
        {
            dsnew.Sales_Rep__c = '00536000000gZzL';
        }
   }
    //loop through and assign Sales Rep for Later Assignment
   for(Distributor_Sales__c dsnew2 : trigger.new)
   {
       if(dsnew2.X3_Digit_Zip__c !=null)
       {
           dsnew2.Sales_Rep_for_Later_Assignment__c  = terrMap2.get(dsnew2.X3_Digit_Zip__c);
    	}   
   			//system.debug('get info - ' +terrMap2.get(dsnew2.X3_Digit_Zip__c));
        if(dsnew2.Sales_Rep_for_Later_Assignment__c == null)
        {
            dsnew2.Sales_Rep_for_Later_Assignment__c = '003360000076rg5';
        }
   }
}

 

All Answers

sfdc_Devlsfdc_Devl
Hi Scott,

Can you try moving the ending parenthesis from line 23 to line 18 and see if that works?

 
SkottypSkottyp
I thought the same thing, but then I get this error: 

Error: Compile Error: Variable does not exist: t.X3_Digit_Zip__c at line 21 column 73
MandyKoolMandyKool
at line 21 where you are getting a compile error 
instead of t.X3_Digit_Zip__c use distsales.X3_Digit_Zip__c
SkottypSkottyp
Here is the error I get with this change: 

AssignDistSales1: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Trigger.AssignDistSales1: line 21, column 1
SkottypSkottyp
Team,

It seems like there is something wrong with my initial loop as I am trying to import 60 unique records with 60 unique zip codes, but the debug log only shows that it runs the loop 9 times. Any ideas?
 
trigger AssignDistSales1 on Distributor_Sales__c ( before insert, before update) 
{
    //get set of 3 digit zips from Distributor Sales to be inserted or updated
    
    Set<String> ZIP = new Set<String>();
    for(Distributor_Sales__c distsales : Trigger.new)
        {
            	//system.debug('distsales - ' + distsales);
            if(distsales.ZipCode__c	!= null){
            	ZIP.add(distsales.X3_Digit_Zip__c);
            	system.debug('dist sales - ' + distsales.X3_Digit_Zip__c);
            }

Here is all I get from this:
Debug Output

Full trigger is listed above!
Thanks
SkottypSkottyp
I'm still having issues with this. I have gotten all the way down to the root insert and there is a basic issue here. I have taken everything out except the first loop to get the IDs and it only loads the first 9 then null the rest. Here is my code:
trigger AssignDistSales2 on Distributor_Sales__c (before insert, before update) {
    //get set of 3 digit zips from Distributor Sales to be inserted or updated
    List<Id> DSids = new List<Id>();
        for(Distributor_Sales__c ds : Trigger.new)
        {
            DSids.add(ds.Id);
           	system.debug('dist sales  ids - ' + ds.Id);
        }
}

And here are my results:
User-added image
SkottypSkottyp
Answering my own question here: 
trigger AssignDistSales2 on Distributor_Sales__c (before insert, before update) 
{
    //get set of 3 digit zips from Distributor Sales to be inserted or updated
    List<String> DSzip = new List<String>();
        for(Distributor_Sales__c ds : Trigger.new)
        {
            if(ds.ZipCode__c.length() != 1)
            {
                DSzip.add(ds.X3_Digit_Zip__c);
                //system.debug('5 digits - ' + ds.X3_Digit_Zip__c);
            }      	
        }
    
    //list of territories needed to map to Distributor Sales zip
    List<Territory__c> terr = [SELECT X3_Digit_Zip__c, Sales_Rep__c FROM Territory__c WHERE X3_Digit_Zip__c in :DSzip];
    List<Territory__c> terr2 =  ([SELECT X3_Digit_Zip__c, Sales_Rep_for_Later_Assignment__c FROM Territory__c WHERE X3_Digit_Zip__c in :DSzip]);
    
    //mapping the territory info for Sales Rep
    Map <String, Id> terrMap = new Map<String, Id>();
    for(Territory__c t : terr)
    {
        terrMap.put(t.X3_Digit_Zip__c, t.Sales_Rep__c);
    }
    
    //mapping the territory info for Sales Rep for later assignment
    Map <String, Id> terrMap2 = new Map<String, Id>();
    for(Territory__c t2 : terr2)
    {
        terrMap2.put(t2.X3_Digit_Zip__c, t2.Sales_Rep_for_Later_Assignment__c);
    }
   
   //loop through and assign Sales Rep
   for(Distributor_Sales__c dsnew : trigger.new)
   {
       if(dsnew.X3_Digit_Zip__c !=null)
       {
           dsnew.Sales_Rep__c  = terrMap.get(dsnew.X3_Digit_Zip__c);
    	}   
   			//system.debug('get info - ' +terrMap.get(dsnew.X3_Digit_Zip__c));
        if(dsnew.Sales_Rep__c == null)
        {
            dsnew.Sales_Rep__c = '00536000000gZzL';
        }
   }
    //loop through and assign Sales Rep for Later Assignment
   for(Distributor_Sales__c dsnew2 : trigger.new)
   {
       if(dsnew2.X3_Digit_Zip__c !=null)
       {
           dsnew2.Sales_Rep_for_Later_Assignment__c  = terrMap2.get(dsnew2.X3_Digit_Zip__c);
    	}   
   			//system.debug('get info - ' +terrMap2.get(dsnew2.X3_Digit_Zip__c));
        if(dsnew2.Sales_Rep_for_Later_Assignment__c == null)
        {
            dsnew2.Sales_Rep_for_Later_Assignment__c = '003360000076rg5';
        }
   }
}

 
This was selected as the best answer