You need to sign in to do that
Don't have an account?
Jenna Hildebrand
Apex Code to Function Like a VLookup
Hello,
I'm working to build a simple, custom territory management solution for my org; other territory management tools don't quite do the trick. Basically, I need to assign accounts to territories based on the zip code value in the account's billing address.
First, some object details: I have a custom object for Territories and a custom object for Zip Codes. Each Zip Code record has a lookup field for a Territory record, which basically signifies that the zip code "belongs" to the related territory. I can go to each Territory record and see a related list of the associated Zip Code records. I also have a Territory lookup field on the account record. (I was told that I may need a Zip Code lookup on the account as well to serve as a junction, but I'm not sure about that one yet...)
Now, what I want to accomplish: I need to create an apex trigger that says, "if the value in the account's billing zip code matches the name of any existing Zip Code record, then 'Territory' (on the account) equals the Territory related to that matched Zip Code record."
I was hoping to accomplish this via process builder, but Salesforce Support informed me that this won't be possible... which brings me here. Can anyone help me get started on coding an apex trigger that can accomplish what I stated above? I'd appreciate any guidance you can provide.
Thank you!
I'm working to build a simple, custom territory management solution for my org; other territory management tools don't quite do the trick. Basically, I need to assign accounts to territories based on the zip code value in the account's billing address.
First, some object details: I have a custom object for Territories and a custom object for Zip Codes. Each Zip Code record has a lookup field for a Territory record, which basically signifies that the zip code "belongs" to the related territory. I can go to each Territory record and see a related list of the associated Zip Code records. I also have a Territory lookup field on the account record. (I was told that I may need a Zip Code lookup on the account as well to serve as a junction, but I'm not sure about that one yet...)
Now, what I want to accomplish: I need to create an apex trigger that says, "if the value in the account's billing zip code matches the name of any existing Zip Code record, then 'Territory' (on the account) equals the Territory related to that matched Zip Code record."
I was hoping to accomplish this via process builder, but Salesforce Support informed me that this won't be possible... which brings me here. Can anyone help me get started on coding an apex trigger that can accomplish what I stated above? I'd appreciate any guidance you can provide.
Thank you!
Used following code to fullfill your requirent,
trigger AssociateAccountToTerritory on Account (before insert, before update) {
Set<string> zipCodeSet = new Set<string>();
for (Account account : Trigger.new) {
if(account.BillingPostalCode != null){
zipCodeSet.add(account.BillingPostalCode);
}
}
List<ZipCode__c> customZipCodeList = new List<ZipCode__c>();
map<string, Id> zipVsTerritoryMap = new map<string, Id>();
if (zipCodeSet.size() > 0) {
customZipCodeList = [Select Id, Name, TerritoryId__c from ZipCode__c where Name IN :zipCodeSet];
if(customZipCodeList.size()>0){
for(ZipCode__c zipObj : zipVsTerritoryMap){
zipVsTerritoryMap.put(zipObj.Name, zipObj.TerritoryId__c);
}
}
for (Account accountObj : Trigger.New) {
if(zipVsTerritoryMap.containsKey(account.BillingPostalCode)){
accountObj.Territory_Id__c = zipVsTerritoryMap.get(accountObj.BillingPostalCode);
}
}
}
}
Thank you,
Amol Salve
Salesforce Develpoer
I used both of your suggestions to help better my understanding of apex. I ended up creating a class to house the logic and a trigger to call the method. Everything seems to be working when I test manually in Sandbox, but I'm having some trouble with the test class I created (I wanted it to work here, too, for peace of mind). Could anyone check it out and see how I need to handle the System.assertEquals logic? That is what's throwing the error.
Below is my TerritoryManagement class: Below is my TerritoryManagementTrigger: Below is my TerritoryManagementTest: The specific error I'm getting is System.AssertException: Assertion Failed: Expected: Test Territory 1, Actual: null
I don't think 'Test Territory 1' is at all correct. What value should go here to match the expected value? Territory_New__c is a lookup, so should it refer to the ID somehow?
Thanks again!
You will then have a refernce to Territory_New__c so you can then check the value. Ideally what you are trying to do is validate the name of the Account territory matches what was automatically inserted via your trigger. You know the name of the Territory should be '12345' so use this as a different approach to check the value:
I feel like the below is getting close to what I'm looking for, but I'm receiving the following error when running the test: System.QueryException: List has no rows for assignment to SObject at line 13, column 1. Is this because the Zip_Code__c query isn't returning anything?
The TerritoryManagement class and trigger are working well in my Sandbox, but I need to make one small modification before rolling it to Production. How can I specify to assign the territory to the account when the account's billing zip code contains the name of one of the Zip Code records? For example, if the Zip Code record is named '12345' and the account's billing zip code is '12345-1234' (the full format), then the territory related to that Zip Code record should still be assigned to the account. Right now, these values would not be recognized as a match.
Here's the section of the class that assigns the territory to the account: Thank you!
Use the left function to get the 5 leftmost characters.
I'm using Data Loader to mass-update some records to test this out. I received the following error:
Sandbox
Apex script unhandled trigger exception by user/organization: 005G0000008GGd4/00DQ000000EeWPG
Source organization: 00DA0000000JfyF (null)
TerritoryManagementTrigger: execution of BeforeUpdate
caused by: System.NullPointerException: Attempt to de-reference a null object
()
Any idea why this could be happening?
Sorry I gave you the lazy man's version.
Try this. There's a hundred ways to do this, so this is just one example.