I have a requirement to make First Name , Middle Name and Last Name fields as initial caps and then form the Name field by concatenating them. Can someone suggest a way out?Please find my trigger here. Sorry for late update. getting below error.

Compile Error: Variable l is used before it is declared. at line 6 column 1


trigger ACCOUNT_AFTER_INSUPD_NAME on Account (after insert , after update) {

  List<Account> acc = new List<Account>();
  acc = [SELECT Id,FirstName,LastName,Middle_vod__c FROM Account WHERE Id IN: Trigger.oldMap.keySet() LIMIT 1];
  for(Account a: acc) {
    String f = a.FirstName;
    List<String> FName= f.split(' ');
    for(Integer i=0;i<FName.size();i++) {
      FName[i] = (FName[i].substring(0,1)).toUppercase() +(FName[i].substring(1,FName[i].length()));
      a.FirstName=a.FirstName+' '+FName[i];

    String m = a.Middle_vod__c;
    List<String> MName= m.split(' ');
    for(Integer i=0;i<MName.size();i++) {
      MName[i] = (MName[i].substring(0,1)).toUppercase() +(MName[i].substring(1,MName[i].length()));
      a.Middle_vod__c=a.Middle_vod__c+' '+MName[i];

    String l = a.LastName;
    List<String> LName= l.split(' ');
    for(Integer i=0;i<LName.size();i++) {
      LName[i] = (LName[i].substring(0,1)).toUppercase() +(LName[i].substring(1,FName[i].length()));
      a.LastName=a.LastName+' '+LName[i];
  update acc;
  • Are these custom fields on Account? – Keith C Dec 12 '14 at 10:47
  • Yes keith. Sorry I forgot to mention. – Testing_SFDC Dec 12 '14 at 10:48
  • Do you want to convert User Data in this format or do you want to enforce User to enter data in this format? – Amit Bangad Dec 12 '14 at 10:51
  • No I want User to enter what he likes. Then I need to incorporate this logic in an after update , after insert Trigger that will do the Need ful changes – Testing_SFDC Dec 12 '14 at 10:54

you can try out something like :

String fName = Acc.FirstName__c.substring(0,1).toUpperCase()+ Acc.FirstName__c.substring(1,Acc.FirstName__c.length()).

Replace FirstName__c with appropriate API name in your code. (Acc is your Account instance for a FOR loop)


  • Hi Amit!! I have put my trigger here. Can you please help me here? Thanks, yes I was using the same logic. – Testing_SFDC Dec 12 '14 at 11:12
  • Sorry for a silly mistake in my code. I have updated my code. It works for me. – Testing_SFDC Dec 12 '14 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.