top of page

First Character of each word in a text using APEX TRIGGER

Here is a Trigger of Before Insert and Before Update to get the First Character of each word in a text or a String. In this Example, I have used to get the Shortcut of the Company Name by getting the first characters in each word so that to store in a seperate field.

Example:

trigger CompanyShort on Company__c (before insert, before update){ String Comp; Integer NumberOfWords; List<String> words; String CompanyShort=''; for(Company__c cc : trigger.new){ Comp = cc.Name; Comp = Comp.toUpperCase(); words = Comp.split('\\s+'); NumberOfWords = words.size(); for(integer i = 0; i < NumberOfWords; i++){ CompanyShort += words[i].substring(0,1); } if(CompanyShort != null){ cc.Company_in_Shortcut__c = CompanyShort; } } }

Featured Posts
Recent Posts
Connect
  • Google+ Long Shadow
  • Facebook Long Shadow
  • LinkedIn Long Shadow
  • Twitter Long Shadow
bottom of page