0

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.

2 Answers 2

1

During the Insert the trigger.old is NULL so this line will throw that error:

Account oldAcc = oldMap.get(acc.Id);

Quickly to resolve it

    for(Account acc : accs)
    {
    Account oldAcc = oldMap == null ? null : 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(oldmap != null && acc.BillingCountry != null && oldAcc.BillingCountry != acc.BillingCountry) {

    acc.Billing_Country_Name__c = country;
}

}

Check for old map being null before you try to use it.

0

Another way to avoid the replication is to reason that in a before trigger like this the updating of BillingCountry to the same value is harmless so the trigger can be simplified to:

trigger updateCountry on Account (before insert, before update) {

    // Update billing country code from list custom setting
    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;                  
        }
    }
}

An example of KISS - a dozen or more lines of code and a class eliminated.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .