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
Andreas DiehlAndreas Diehl 

Apex Class and Trigger to Update Industry Title based on SIC Code

Based on the provided SIC code I want to update a field with the respective Industry Title. Since there are too many SIC Codes to work with formula fields (exceeding field limits) I am left with Apex. 

I  have list of SIC Codes and their corresponding Industry Title; 
lets say 

​​​​​​​1 = Industry A
2 = Industry B
3 = Industry C


Can you help with an Apex Class that uses the SWITCH statement to update a text field 'Industry_Title__c' (Text) based on a given value in the field 'SIC_Code__c' (Integer).

And can you help with a Trigger that executes the above class on every field change of 'SIC_Code__c'.
Thanks guys!

Best Answer chosen by Andreas Diehl
SarvaniSarvani
Hi Andreas,

Please try with below sample code. This works when the Account record's SIC_Code__c field value is changed. (Change the code according to your requirements).

Trigger code:
trigger AccountSwitch on Account (before update) {
    
    for (Account Acc : Trigger.new) {
    Account old = Trigger.oldMap.get(Acc.Id);
    if (Acc.SIC_Code__c!= old.SIC_Code__c) {
        Switchclass.method1(Trigger.new);
    }
}
 }
Apex class code:
public class switchclass{
public static void method1(list<Account> Acc){
    list<Account> Accountlist=new list<Account>();
    for(Account Accrec:Acc){
        if(Accrec.SIC_Code__c!=NULL){
            Accountlist.add(Accrec);
        }
    }
    for(Account EachRec:Accountlist){
        integer i= EachRec.SIC_Code__c;
        switch on i{
            when 1{
                EachRec.Industry_Title__c='Industry A';
            }
            when 2{
                EachRec.Industry_Title__c='Industry B';
            }
            when 3{ 
                EachRec.Industry_Title__c='Industry C'; 
            }

      //Add your other conditions for different SIC_code__c values
        }
    }
}
}
Hope this helps! Please mark as best if it does.

Thanks

All Answers

SarvaniSarvani
Hi Andreas,

Please try with below sample code. This works when the Account record's SIC_Code__c field value is changed. (Change the code according to your requirements).

Trigger code:
trigger AccountSwitch on Account (before update) {
    
    for (Account Acc : Trigger.new) {
    Account old = Trigger.oldMap.get(Acc.Id);
    if (Acc.SIC_Code__c!= old.SIC_Code__c) {
        Switchclass.method1(Trigger.new);
    }
}
 }
Apex class code:
public class switchclass{
public static void method1(list<Account> Acc){
    list<Account> Accountlist=new list<Account>();
    for(Account Accrec:Acc){
        if(Accrec.SIC_Code__c!=NULL){
            Accountlist.add(Accrec);
        }
    }
    for(Account EachRec:Accountlist){
        integer i= EachRec.SIC_Code__c;
        switch on i{
            when 1{
                EachRec.Industry_Title__c='Industry A';
            }
            when 2{
                EachRec.Industry_Title__c='Industry B';
            }
            when 3{ 
                EachRec.Industry_Title__c='Industry C'; 
            }

      //Add your other conditions for different SIC_code__c values
        }
    }
}
}
Hope this helps! Please mark as best if it does.

Thanks
This was selected as the best answer
Andreas DiehlAndreas Diehl

Hi Sarvani,

just tested the proposed code and it works exactly as I requested. Thanks a lot!
Am I correct that I have to have a related test class when I want to deploy that to production!? How would that look like?

Really appreciate the help here!

SarvaniSarvani
Hi Andreas,

Correct if you are trying to deploy production you need test class. Please try below code as a test class.
 
@isTest 
public class TestClassforSwitchClass {
    @isTest public static void method(){
        Account Acc=new Account();
        Acc.name='Test Account';
        Acc.SIC_Code__c=1; // Update SIC_Code__c with values you have
        insert Acc;
        Account UpdatedAcc=[select id,name,SIC_Code__c, Industry_Title__c from account where id=:Acc.id];
        UpdatedAcc.SIC_Code__c =2; // Update SIC_Code__c with values you have
        update UpdatedAcc;       
    }

}

Hope this helps!

Thanks