Capitalize First Name, LastName, and All Address Fields of Account

Trigger On Account Object

trigger ChangingFirstlttrUpper on Account (before insert,before update) {
   ChanignToCaps.Change(trigger.new);
      if(trigger.isBefore )
         if(trigger.IsInsert ||trigger.IsUpdate )
           ChanignToCaps.Change(trigger.new);
} 

Apex Class

global class ChanignToCaps{
   global static void Change( Account [] acc){
      for(Account a :acc){
         if(a.Name!=null || a.Name != ' '){
           a.name= formatToUpper(a.name);
   }
   if(a.BillingStreet != null )
    a.BillingStreet = formatToUpper(a.BillingStreet);

   if(a.BillingCity !=null)
    a.BillingCity = formatToUpper(a.BillingCity);

   if(a.BillingState !=null )
    a.BillingState= formatToUpper(a.BillingState);

   if(a.BillingPostalCode !=null )
    a.BillingPostalCode = formatToUpper(a.BillingPostalCode);

   if(a.BillingCountry !=null )
    a.BillingCountry= formatToUpper(a.BillingCountry);
   }
}

formatToUpper Is A Static Function Which Does The Acutal Work:

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;
}
}
Capitalize First Name, LastName, and All Address Fields of Account

Capitalize First Name, LastName, and All Address Fields of Account

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s