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
John GardinerJohn 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');

 
Best Answer chosen by John Gardiner
Amit Singh 1Amit Singh 1
Hi Jhon,

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.
 
trigger branchUpdate on Contact (before insert) {

    Map<String,String> branchMap = new map<string,string>();
    For(Branch__c b : [Select Id, Name, Branch__c From Branch__c]){
        If(b.Name!=null && b.Branch__c!=null && b.Name!='' && b.Branch__c!=''){
             branchMap.put(b.Name, b.Branch__c);// MailingCountry as Key and branch as Value
        }
    }// Branch__c is custom Object

// Iterate through Contacts and populate Branch
For(Contact c: Trigger.Name){
   If(c.MailingCountry!=null && c.MailingCountry!=''){
      If(branchMap!=null && branchMap.containsKey(c.MailingCountry)){
         c.Branch__c = nchMap.get(c.MailingCountry);// Branch__c is custom field
      }
   }
}


}
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

Amit Singh 1Amit Singh 1
Hi Jhon,

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.
 
trigger branchUpdate on Contact (before insert) {

    Map<String,String> branchMap = new map<string,string>();
    For(Branch__c b : [Select Id, Name, Branch__c From Branch__c]){
        If(b.Name!=null && b.Branch__c!=null && b.Name!='' && b.Branch__c!=''){
             branchMap.put(b.Name, b.Branch__c);// MailingCountry as Key and branch as Value
        }
    }// Branch__c is custom Object

// Iterate through Contacts and populate Branch
For(Contact c: Trigger.Name){
   If(c.MailingCountry!=null && c.MailingCountry!=''){
      If(branchMap!=null && branchMap.containsKey(c.MailingCountry)){
         c.Branch__c = nchMap.get(c.MailingCountry);// Branch__c is custom field
      }
   }
}


}
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
 
This was selected as the best answer
John GardinerJohn Gardiner
Thank you! I think this might just work!
John GardinerJohn Gardiner
Hi Amit,

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?
trigger branchUpdate on Contact (before insert, before update) {

    Map<String,String> branchMap = new map<string,string>();
    For(Branch__c b : [Select Id, Name, Branch__c From Branch__c]){
        If(b.Name!=null && b.Branch__c!=null && b.Name!='' && b.Branch__c!=''){
            branchMap.put(b.Name, b.Branch__c); //Contact MailingCountry as key and Branch as Value
        }
        }// Branch__c is a custom Object
    
    //Iterate through Contacts and auto populate Branch
    For(Contact c: Trigger.New){
        If(c.MailingCountry!=null && c.MailingCountry!='' {
            If(branchMap!=null && branchMap.containsKey(c.MailingCountry)){
                c.Branch__c = branchMap.get(c.MailingCountry); //Branch__c is a custom field
            }           
            }
        If(c.MailingCountry==null && c.MailingCountry=='' {
            c.Branch__c = 'United States';
        }
        
    }
}

 
Amit Singh 1Amit Singh 1
Try Below code.
 
trigger branchUpdate on Contact (before insert, before update) {

    Map<String,String> branchMap = new map<string,string>();
    For(Branch__c b : [Select Id, Name, Branch__c From Branch__c]){
        If(b.Name!=null && b.Branch__c!=null && b.Name!='' && b.Branch__c!=''){
            branchMap.put(b.Name, b.Branch__c); //Contact MailingCountry as key and Branch as Value
        }
        }// Branch__c is a custom Object
    
    //Iterate through Contacts and auto populate Branch
    For(Contact c: Trigger.New){
        If(c.MailingCountry!=null && c.MailingCountry!='') {
            If(branchMap!=null && branchMap.containsKey(c.MailingCountry)){
                c.Branch__c = branchMap.get(c.MailingCountry); //Branch__c is a custom field
            }           
            }
        If(c.MailingCountry==null && c.MailingCountry=='') {
            c.Branch__c = 'United States';
        }
        
    }
}
Let me know the outcomes.
Thanks,
Amit Singh