+ Start a Discussion
BhatiaBhatia 

Trigger for capitalizing the first letter of first name and last name on contacts

how can i write a trigger to change the first letter capital for  existing  first name and last name on the Contact standard object.

 

Any help will be appreicable.

 

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Hi,

 

If you want to do this for newly created or updated contact record then your trigger is

 

trigger CapitalizeName on Contact( before insert , before update)
    {
        for(Contact c : Trigger.new)
            {
                if(c.FirstName != null)
                    c.FirstName =  c.FirstName.subString(0 ,1).ToUpperCase() +  c.FirstName.subString(1);
                    
                c.LastName =  c.LastName.subString(0 ,1).ToUpperCase() +  c.LastName.subString(1);
            }
    }

 For existing record just write batch class and update contacts in batch, that update will fire this trigger and there anme will also get set like this.

All Answers

Shashikant SharmaShashikant Sharma

Hi,

 

If you want to do this for newly created or updated contact record then your trigger is

 

trigger CapitalizeName on Contact( before insert , before update)
    {
        for(Contact c : Trigger.new)
            {
                if(c.FirstName != null)
                    c.FirstName =  c.FirstName.subString(0 ,1).ToUpperCase() +  c.FirstName.subString(1);
                    
                c.LastName =  c.LastName.subString(0 ,1).ToUpperCase() +  c.LastName.subString(1);
            }
    }

 For existing record just write batch class and update contacts in batch, that update will fire this trigger and there anme will also get set like this.

This was selected as the best answer
BhatiaBhatia

Thanks Shashank  it works.

You are really working for the salesforce, For existing records what i was mainly required.

BhatiaBhatia

Finally i have built it what it was desired.

 

Thanks a lot shashank again to you for guiding me to achieve the goal.

 

Keep doing it.

 

Very much thanks to you.

SteveBowerSteveBower

Just fyi, I find it always useful to think about filtering the records that your trigger is going to run on.  After all, simple IF statements are cheap, substrings and unnecessary updates are expensive.   It's even more important when your trigger would have some more expensive database operations for each record.  And it's just a good practice.

 

Best, Steve.

 

trigger CapitalizeName on Contact( before insert , before update) {
     
   for(Contact c : Trigger.new) {
        // If this is an update, but the First and Last Names haven't been changed, skip this.
        // Note that putting the isUpdate check first will short-circuit the evaluation of the
        // rest of this IF statement if it's not an update.
        if (trigger.isUpdate && 
            c.FirstName == Trigger.oldMap.get(c.id).FirstName && 
            c.LastName == Trigger.oldMap.get(c.id).LastName
        ) continue;

        if(c.FirstName != null) c.FirstName = c.FirstName.subString(0,1).ToUpperCase() + c.FirstName.subString(1);
        c.LastName =  c.LastName.subString(0,1).ToUpperCase() + c.LastName.subString(1);
   }

}

 

 

 

 

 

Shashikant SharmaShashikant Sharma

Hi Steave,

 

I understand your point that triggers should always execute after checking the entry criteria, I have written this on my blog as well. http://forceschool.blogspot.com/2011/05/writing-apex-trigger-issues-and.html

But here one of the demand was also for doing this for existing records. Thats why I did not put any entry criteria here. I know even existing records handling from batch can also be handled using a static variable, but I thought to keep it simple right now.

BhatiaBhatia

Thanks Steve and Shaishank both for giving such useful tips .

ŁukaszŁukasz

I recently had the same problem but it was more complicated as I had to also support dash character. In this link you can find described solution for this problem:

http://blog.enxoo.com/en/2013/06/automatically-capitalize-name-first-letters/

Elizabeth Gomez 5Elizabeth Gomez 5
Shaishank, so just want to make sure I understand -- what you have posted is going to capitalize the first letter of the first name and first letter of the last name automatically? Or it's only for newly updated contacts? 
VIP Condos TorontoVIP Condos Toronto

@tukasz What was the issue with the Dash character? I have some records where the name is missing and replaced with a dash character so now you have me concerned ;-)  But your link was not working,

Thanks!