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
Katherine RoweKatherine Rowe 

Best way to map values in a trigger - case vs else if

I've got the following trigger working, to map the value of an industry to a different industry:
 
trigger MapDataCOMIndustry on Account (before insert, before update) {
    for(Account a1: Trigger.new){
        if(a1.industry=='Government'){
           a1.industry='Emergency Services';
        }
    }  
}

Now I have to do that for a long list of industries, see below. What is the best way to write this in the trigger? Can I use something like a "case" method? Or does it need to be a bunch of else if statements?
 
trigger MapDataCOMIndustry on Account (before insert, before update) {
    for(Account a1: Trigger.new){
    
        if(a1.industry=='Government'){
           a1.industry='Emergency Services';
        }
        
        else if(a1.industry=='Agriculture'){
           a1.industry='Agriculture & Fisheries';
        }
        
        else if(a1.industry=='Apparel'){
            a1.industry='Other';
        }
        
        else if(a1.industry=='Banking'){
            a1.industry='Finance';
        }
    }  
}



User-added image

 
Best Answer chosen by Katherine Rowe
Edwin VijayEdwin Vijay
Please make this change to the code
trigger MapDataCOMIndustry on Account (before insert, before update) {
    for(Account a1: Trigger.new){
        if(CustomMappings__c.getInstance(a.Industry) != null){
           a1.industry=CustomMappings__c.getInstance(a.Industry).HSI_Industry__c;
        }
    }  
}

 

All Answers

Edwin VijayEdwin Vijay
Load this table into custom settings. 

CustomMappings__c: Name = Salesforce standard industry
                                 HSI__c = HSI Industry

you can then get these vales directly using
 
trigger MapDataCOMIndustry on Account (before insert, before update) {
    for(Account a1: Trigger.new){
        if(CustomMappings__c.getInstance(a.Industry) != null){
           a1.industry=CustomMappings__c.getInstance(a.Industry);
        }
    }  
}

If that helps, please mark it solved.
Katherine RoweKatherine Rowe
What's it upset at in this error?

User-added image
Katherine RoweKatherine Rowe
Also, I'm pretty new to custom settings... if this is what my custom setting looks like, how would that be reflected in my trigger code? I think I would have to reference "HSI_Industry__c" somewhere in the trigger?

User-added image



User-added image

 
Edwin VijayEdwin Vijay
Please make this change to the code
trigger MapDataCOMIndustry on Account (before insert, before update) {
    for(Account a1: Trigger.new){
        if(CustomMappings__c.getInstance(a.Industry) != null){
           a1.industry=CustomMappings__c.getInstance(a.Industry).HSI_Industry__c;
        }
    }  
}

 
This was selected as the best answer
Katherine RoweKatherine Rowe
Awesome, that did it, thanks! And that makes sense now that I look at it.
Katherine RoweKatherine Rowe
Edwin,

A followup question... when using a custom setting for mapping, what do I do when I need the left value to be more than 40 characters? Because the name field won't let me go above 40. When I use the getInstance() method in my trigger, does it have to reference the name field, or can I use some other custom field that I make bigger than 40 characters?

For example, the first one below has more than 40 characters and it won't let me load it into the name field.

(left value) -> (right value)
"abcdefghijklmnopqrstuvwxyzbcdefghijklmnopqrstuvwxyz" -> "apple"
"abcdef" -> "banana"
"abcdefghijklmn" -> "grape"