You need to sign in to do that
Don't have an account?
SF Beginner 2019
Attempt to deReference a Null Object when billing country is null please help
global class ChangingCapsLock{ global static void Change( Account [] acc){ for(Account a :acc){ if(a.BillingPostalCode !=null ) a.BillingPostalCode = allUpper(a.BillingPostalCode); if(a.BillingCountry !=null ) a.BillingCountry= formatToUpper(a.BillingCountry); a.BillingCountry= formatToUpperLower(a.BillingCountry); if(a.BillingCountry ==null ) String.isEmpty(a.BillingCountry); if(a.ShippingPostalCode !=null ) a.ShippingPostalCode = allUpper(a.ShippingPostalCode); if(a.ShippingCountry !=null ) a.ShippingCountry= formatToUpper(a.ShippingCountry); a.ShippingCountry= formatToUpperLower(a.ShippingCountry); } } public static String formatToUpper (String Str) { String result = ''; for (String iter : Str.split('[ ]+')) { if (iter != null && iter != '') { if (iter.length() > 1) { result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()) + ' '; } else result += iter.substring(0,1).toUpperCase() + ' '; } } return result; } public static String allUpper (String Str) { String result = ''; for (String iter : Str.split('[ ]+')) { if (iter != null && iter != '') { if (iter.length() > 1) { result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()).toUpperCase() + ' '; } else result += iter.substring(0,1).toUpperCase() + ' '; } } return result; } public static String formatToUpperLower (String Str) { String result = ''; for (String iter : Str.split('[ ]+')) { if (iter != null && iter != '') { if (iter.length() > 1) { result += iter.substring(0,1).toUpperCase() + iter.substring(1,iter.length()).toLowerCase() + ' '; } else result += iter.substring(0,1).toLowercase() + ' '; } } return result; } }
trigger AccountChangerFirstLetter on Account (before insert, before update) { ChangingCapsLock.Change(trigger.new); if(trigger.isBefore ) if(trigger.IsInsert ||trigger.IsUpdate ) ChangingCapsLock.Change(trigger.new); }
Just add the if condition in a block. Like, replace
if(a.BillingCountry !=null )
to
if(a.BillingCountry !=null ){
}
When a field is null, if you set it to empty ("), then it will stay as null in the backend. It is the default behaviour. So, When it is null, leave it like that