You need to sign in to do that
Don't have an account?
rhp
Getting a custom field value into account's object using zip code
I'm writting a bulkified trigger to get a value from a custom object into account's object depending on the zip code.
Trigger:
trigger Terri on Account (before insert, before update) {
Set<String> Zips = new Set<String>();
for(Account a: Trigger.new)
Zips.add(a.BillingPostalCode);
Map<String, TerritoryZip__c> teri = new Map<String, TerritoryZip__c>([Select Territory__c from TerritoryZip__c Where ZipCode__c = :Zips]);
for (Account a : Trigger.new)
a.Territory__c = teri.get(a.BillingPostalCode).Territory__c;
}
Error:
Terri: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Terri: line 11, column 1
what should I do now?
JeRun is correct, your map definition has changed you will need to change:
a.Te__c = postCodeTeriMap.get(a.BillingPostalCode).Territory
__c;
to
a.Te__c = postCodeTeriMap.get(a.BillingPostalCode);
When you do the get it will return the string you placed in the map
All Answers
Your error is because your query that populated the map did not include the record you were getting from the map.
Add some system.debug statements on the map, zips, etc and make sure they are holding the values you need.
Hello Starz26,
I checked the value of te map[teri] and the values inside Zips s following:
system.debug(teri);
system.debug(Zips);
It gave me the right values.
The line that htrows error is: a.Territory__c = teri.get(a.BillingPostalCode).ZipCode__c;
What should I do?
Thanks.
Hey Starz,
i found the error but need your help.
Instead of creating a id and value in map it creates id and extra elements along with tha values.
Example:
where I need only
Not sure how your are wanting to build your map.
They way you have it the KEY is the ID of the Zip Code record.
You are getting a dereference error because you are asking the map to get the record where the KEY is the billing postal code and there is no key for that.
If you want to build the map with anything other than the ID => Record you will have to use a loop to build it i.e.:
For(Account a : [Select ExternalID__c, Name From Account])
mapAccount.put(a.ExternalID__c, a);
The above will create a map using the ExternalID__c field as the key and the account record as the value.
Thanks. I'm a begginer and trying to understand the concepts.
After editing I'm geting an error.
Compile Error: Initial term of field expression must be a concrete SObject: String
Trigger:
trigger Terri on Account (before insert, before update) {
Set<String> Zips = new Set<String>();
Map<String, String> postCodeTeriMap = new Map<String, String>();
for(Account a: Trigger.new)
Zips.add(a.BillingPostalCode);
for(TerritoryZip__c ter : [Select Id, Name, ZipCode__c, Territory__c from TerritoryZip__c Where ZipCode__c IN: zips]) {
postCodeTeriMap.put(ter.ZipCode__c, ter.Territory__c);
}
for (Account a : Trigger.new)
a.Te__c = postCodeTeriMap.get(a.BillingPostalCode).Territory__c; [ERROR IN THIS LINE]
}
How to solve this?
Thanks
postCodeTeriMap is now a map of string which holds the territory data. You cannot dereference it again like this "postCodeTeriMap .get(a.BillingPostalCode).Territory__c". You will have to use it as "postCodeTeriMap.get(a.BillingPostalCode)"
JeRun is correct, your map definition has changed you will need to change:
a.Te__c = postCodeTeriMap.get(a.BillingPostalCode).Territory
__c;
to
a.Te__c = postCodeTeriMap.get(a.BillingPostalCode);
When you do the get it will return the string you placed in the map
Thank you so much Guys. It worked.