You need to sign in to do that
Don't have an account?
sfdcdev
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.
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.
All Answers
Getting following error: Compile Error: Illegal assignment from Boolean to String at line 19 column 21
Thanks.I got it