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
sfdcdevsfdcdev 

Convert a trigger in to Helper Class

I have written a trigger to update Billing_Country_Name__c custom field on Account.Country Codes are being saved in custom settings.

    trigger updateCountry on Account (before insert, before update) {
  
    if(Trigger.isBefore) {
      
        if(Trigger.isInsert) {
          for(Account acc : Trigger.new) {
              if(acc.BillingCountry != null) {
                  string country = Country_ISO_Codes__c.getInstance(acc.BillingCountry).Two_Digit_ISO_Code__c;
                  acc.Billing_Country_Name__c = country;                  
              }
                
          }
      }else
      if(Trigger.isUpdate) {
        for(Account acc : Trigger.new) {
            Account oldAccs = Trigger.oldMap.get(acc.Id);
        
            if(acc.BillingCountry != null && oldAccs.BillingCountry != acc.BillingCountry) {
                string country = Country_ISO_Codes__c.getInstance(acc.BillingCountry).Two_Digit_ISO_Code__c;
                acc.Billing_Country_Name__c = country;  
            }
         }
             
       }
    }
      
}

As you can see there are duplicate codes in Trigger.isInsert and Trigger.isUpdate.I seperated the logic in to helper class and getting null pointer error.

Helper Class-

    public with sharing class TriggerHelper {

      public static void updateAccountBillingCountry(List<Account> accs, Map<Id, Account> oldMap) {
  
        for(Account acc : accs)
        {
            Account oldAcc = oldMap.get(acc.Id);
            String country = Country_ISO_Codes__c.getInstance(acc.BillingCountry).Two_Digit_ISO_Code__c;
            
            f(acc.BillingCountry != null) {
              acc.Billing_Country_Name__c = country;
            }else
            if(acc.BillingCountry != null && oldAcc.BillingCountry != acc.BillingCountry) {
                  
                acc.Billing_Country_Name__c = country;
            }
            
        }

        
   }
}

Trigger-

    trigger updateCountry on Account (before insert, before update) {
      
      if(Trigger.isBefore) {
         if(Trigger.isInsert || Trigger.isUpdate) {
           TriggerHelper.updateAccountBillingCountry(Trigger.new, Trigger.oldMap);
         }
         
      }
}

The trigger is working fine when updating a record, but it's not working when I inserts a record.Also, I don't know, How to check for separate if condition as done in my single trigger.
Best Answer chosen by sfdcdev
Nayana KNayana K
Helper Class-

    public with sharing class TriggerHelper {

      public static void updateAccountBillingCountry(List<Account> accs, Map<Id, Account> oldMap) {
	  
	    Map<String, String> mapCodeToCountry = new Map<String, String>();
		/* instead of placing Country_ISO_Codes__c.getInstance(acc.BillingCountry).Two_Digit_ISO_Code__c; line in for loop . Better fetch all custom setting value and keep in map*/
		for(Country_ISO_Codes__c objCountryCode : Country_ISO_Codes__c.getall().values())
		{
			mapCodeToCountry.put(objCountryCode.FIELD_API_NAME_OF_COUNTRY , objCountryCode.Two_Digit_ISO_Code__c);
		}
		
		String country = '';
        for(Account acc : accs)
        {
			if(acc.BillingCountry != null && (Trigger.isInsert || (Trigger.isUpdate && oldMap.get(acc.Id).BillingCountry != acc.BillingCountry)))
			{
				if(mapCodeToCountry.containsKey(acc.BillingCountry))
				{
					acc.Billing_Country_Name__c = mapCodeToCountry.containsKey(acc.BillingCountry);
				}
			}  
        }  
   }
}

 

All Answers

Nayana KNayana K
Helper Class-

    public with sharing class TriggerHelper {

      public static void updateAccountBillingCountry(List<Account> accs, Map<Id, Account> oldMap) {
	  
	    Map<String, String> mapCodeToCountry = new Map<String, String>();
		/* instead of placing Country_ISO_Codes__c.getInstance(acc.BillingCountry).Two_Digit_ISO_Code__c; line in for loop . Better fetch all custom setting value and keep in map*/
		for(Country_ISO_Codes__c objCountryCode : Country_ISO_Codes__c.getall().values())
		{
			mapCodeToCountry.put(objCountryCode.FIELD_API_NAME_OF_COUNTRY , objCountryCode.Two_Digit_ISO_Code__c);
		}
		
		String country = '';
        for(Account acc : accs)
        {
			if(acc.BillingCountry != null && (Trigger.isInsert || (Trigger.isUpdate && oldMap.get(acc.Id).BillingCountry != acc.BillingCountry)))
			{
				if(mapCodeToCountry.containsKey(acc.BillingCountry))
				{
					acc.Billing_Country_Name__c = mapCodeToCountry.containsKey(acc.BillingCountry);
				}
			}  
        }  
   }
}

 
This was selected as the best answer
sfdcdevsfdcdev
Hi Nayana,

Getting following error: Compile Error: Illegal assignment from Boolean to String at line 19 column 21
sfdcdevsfdcdev
Hi Nayana,

Thanks.I got it