You need to sign in to do that
Don't have an account?

bulk lead trigger error - Attempt to de-reference a null object
I'm writing a bulk trigger to update lead owners based on a zip code table we have.
I think I'm close to having the whole thing working, but I'm getting the "Attempt to de-reference a null object" error when I actually execute a test of my code.
Here is the code:
There must be some small problem I have in here. Basically what I want to do is find the Sales Rep who corresponds to the new lead's zip code and assign that sales rep as the new lead OWNER.
If anyone knows why I'm getting that error and can give me some advice I would be much obliged.
Thanks
I think I'm close to having the whole thing working, but I'm getting the "Attempt to de-reference a null object" error when I actually execute a test of my code.
Here is the code:
Code:
trigger LeadAssignmentTrigger on Lead (before insert) { List<String> zips = new List<String>(); // Loop through Leads and create list of zip codes for (Lead lead : Trigger.new) { if (lead.PostalCode != NULL) { zips.add(lead.PostalCode); } } //query zip code table and link zips to Sales reps // Name field = a single zip code
Map<String, Zip_Code__c> leadsToUpdate = new Map<String, Zip_Code__c>
([select Name, Sales_Rep__c from Zip_Code__c where Name in :zips]);
// If there is at least one match, bulk update
if (leadsToUpdate.size() > 0)
{
for (Lead lead : Trigger.new)
{
//assign the lead owner to the zip code owner
lead.OwnerId = leadsToUpdate.get(lead.PostalCode).Sales_Rep__c; //this line gives the error
}
}
}
There must be some small problem I have in here. Basically what I want to do is find the Sales Rep who corresponds to the new lead's zip code and assign that sales rep as the new lead OWNER.
If anyone knows why I'm getting that error and can give me some advice I would be much obliged.
Thanks
I was thinking that because you had a query in the for loop that it wouldn't be bulk loadable. But what I missed is that the query only has to happen once in that scenario.
For all the other Map newbies out there, once I finally understood what Simon was saying above, everything was clear as day: you can still use a SOQL query to figure out what your Map is, but you have to loop through and setup the Key yourself or otherwise the query will automatically assign the object ID as your key and that's probably not what you want.
Here is my working code to reassign (in bulk, if necessary) the lead owner id to a more appropriate one based on their zip code. Hope this is helpful:
All Answers
Is that wrong?
Jim, thanks for your tip - I'll add in another check to make sure I handle null values once I get it working.
Any advice would be appreciated.
Thanks
map<String, Account> acc = new map<String, Account>( [select name, accountNumber from account limit 5]);
System.debug(acc);
in the log you'll see something like
09:05:57 INFO - 20081202170557.318:AnonymousBlock: line 1, column 1: Anonymous
20081202170557.318:AnonymousBlock: line 1, column 5: DeclareVar: MAP:String,SOBJECT:Account acc
20081202170557.318:AnonymousBlock: line 1, column 54: SOQL query with 5 rows finished in 8 ms
20081202170557.318:AnonymousBlock: line 1, column 5: initial value: {0018000000MLx2VAAT=Account:{AccountNumber=CC978213, Name=GenePoint, Id=0018000000MLx2VAAT}, 0018000000MLx2WAAT=Account:{AccountNumber=CD355119-A, Name=United Oil & Gas, UK, Id=0018000000MLx2WAAT}, 0018000000MlWi0AAF=Account:{AccountNumber=r123, Name=rotor inc., Id=0018000000MlWi0AAF}, 0018000000Mm4QsAAJ=Account:{AccountNumber=JR1, Name=John's Rotors, Id=0018000000Mm4QsAAJ}, 0018000000Mn7AhAAJ=Account:{Name=Orcsweb, Id=0018000000Mn7AhAAJ}}
20081202170557.318:AnonymousBlock: line 2, column 1: System.debug(MAP:String,SOBJECT:Account)
20081202170557.318:AnonymousBlock: line 2, column 1: {0018000000MLx2VAAT=Account:{AccountNumber=CC978213, Name=GenePoint, Id=0018000000MLx2VAAT}, 0018000000MLx2WAAT=Account:{AccountNumber=CD355119-A, Name=United Oil & Gas, UK, Id=0018000000MLx2WAAT}, 0018000000MlWi0AAF=Account:{AccountNumber=r123, Name=rotor inc., Id=0018000000MlWi0AAF}, 0018000000Mm4QsAAJ=Account:{AccountNumber=JR1, Name=John's Rotors, Id=0018000000Mm4QsAAJ}, 0018000000Mn7AhAAJ=Account:{Name=Orcsweb, Id=0018000000Mn7AhAAJ}}
See how the map keys are the Id of the account.
Message Edited by SimonF on 12-02-2008 09:07 AM
If I have a map that looks like this:
< Zip Code, Sales Rep>
< 44333, Jim Smith>
< 44222, Mike Johnson>
How can I loop through my newly created Leads and assign the right zip code to the sales rep? My lead object has no clue what the IDs are of the Zip Code object. I thought that was the point of using the Map's get() function and setting up a map in the first place.
Thanks
Simon says I need to somehow reference the ID of the map instead of being able to use the Zip code string as the ID and do a lookup on that.
My question is how do I do that? As I loop through my Leads being inserted, I only know what the zip codes are of each lead, I can easily map that zip to a sales rep based on my zip code object, but I'm having trouble doing the final assignment of the Sales Rep ID (User lookup) to the Lead Owner ID.
Does anyone know what I'm doing wrong here?
<90210, UserID (such as 00570000000mbXX)>
So my table looks like that but I'm still trying to decipher what Simon said about how I can't do the lookup using the zip code, I have to use the UserID key or something like that. It doesn't make sense to me based on all the of Map documentation I've read.
Can anyone assist in this and tell me why this line of code doesn't work?
Thanks
http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=342#M342
Try that and see if you have better results.
Post the output of the system log when you execute your test if you still have problems.
I was thinking that because you had a query in the for loop that it wouldn't be bulk loadable. But what I missed is that the query only has to happen once in that scenario.
For all the other Map newbies out there, once I finally understood what Simon was saying above, everything was clear as day: you can still use a SOQL query to figure out what your Map is, but you have to loop through and setup the Key yourself or otherwise the query will automatically assign the object ID as your key and that's probably not what you want.
Here is my working code to reassign (in bulk, if necessary) the lead owner id to a more appropriate one based on their zip code. Hope this is helpful:
John,
There are only 2 loop types in your code (3 loops total). Two of them are on the Lead object,which is standard and must exist.
The other is on the Zip_Code__c custom object. Does that exist? Is it the correct API Name?