You need to sign in to do that
Don't have an account?
John Gardiner
Update field with values from map
Hi All,
I am pretty new to Apex and am hoping someone can give me some assistance. I have a custom field on the Contact object called "branch". Currently, we are manually entering the "branch" each contact belongs to based on their location. My goal is to automate this task with a trigger by referencing the MailingCountry on the contact record, but I can't seem to figure it out.
I started attempting this using a map, but am not sure that's the best way, and didn't get very far. My intention, for example, is if the MailingCountry is Chad, id like the branch field to populate with Africa. I've pasted below where I got started.
trigger branchUpdate on Contact (before insert) {
map<String,String> bm = new map<string,string>();
bm.put('Chad','Africa');
bm.put('United States','United States');
I am pretty new to Apex and am hoping someone can give me some assistance. I have a custom field on the Contact object called "branch". Currently, we are manually entering the "branch" each contact belongs to based on their location. My goal is to automate this task with a trigger by referencing the MailingCountry on the contact record, but I can't seem to figure it out.
I started attempting this using a map, but am not sure that's the best way, and didn't get very far. My intention, for example, is if the MailingCountry is Chad, id like the branch field to populate with Africa. I've pasted below where I got started.
trigger branchUpdate on Contact (before insert) {
map<String,String> bm = new map<string,string>();
bm.put('Chad','Africa');
bm.put('United States','United States');
May I suggest you to create a new Object with only one Custom field Named as Branch and use Name Standard Field. Then insert all the records on that object for example like - Name = 'Chad' which will be equals as MailingCountry and in custom field Branch__c = 'Africa'.
and then use below code for trigger.
By using new Custom object you do not need to modify ur trigger again and again in future u can add as many branch u want.
Let me know the outcomes.
Thanks,
Amit Singh
All Answers
May I suggest you to create a new Object with only one Custom field Named as Branch and use Name Standard Field. Then insert all the records on that object for example like - Name = 'Chad' which will be equals as MailingCountry and in custom field Branch__c = 'Africa'.
and then use below code for trigger.
By using new Custom object you do not need to modify ur trigger again and again in future u can add as many branch u want.
Let me know the outcomes.
Thanks,
Amit Singh
The end user has now requested that if the MailingCountry is blank or if the country does not have a specific branch to auto fill the branch field to 'United States'. I've been trying to do this by adding an additional if statement but I'm not having any luck. Could you please take a look?
Let me know the outcomes.
Thanks,
Amit Singh