Normalize Text In Salesforce Using Apex

Hello There! Welcome Back 🙂
Hope you are doing great 😀

Today we are going to take a look at Normalizing Text in Salesforce Using Apex.

Real-Time Use-Case
A salesforce org has around 100 users setup but, the users’ First Name and Last Name are all in caps(E.g. FIRSTNAME LASTNAME) and we have to normalize the First Name and Last Name to be capitalised(E.g. Firstname Lastname)
So, going to each user record and rewriting the firstname & lastname is not the ideal solution.
The below code helps us achieve all of it in just a few questions.

Solution
Developer Console → Execute Anonymous(ctrl+e for windows, command+e for mac)

List<User> usrs = [Select Id, FirstName, LastName from User];
List<User> usrsToUpdate = new List<User>();

for(User u: usrs){
    string fName = u.FirstName;
    string lName = u.LastName;
    boolean up = false;
    if(fName != null && fName.isAllUpperCase()){
        u.FirstName = fName.toLowerCase().capitalize();
        up = true;
    }
    if(lName != null && lName.isAllUpperCase()){
        u.LastName = lName.toLowerCase().capitalize();
        up = true;
    }
    if(up == true){
        usrsToUpdate.add(u);
    }
}

if(usrsToUpdate.size() > 0){
    system.debug(usrsToUpdate);
	update usrsToUpdate;
}

Result
FIRSTNAME LASTNAME → Firstname Lastname

That is it! We learnt how to normalize the text of huge data in just a few seconds.
Awesome, Right?

If you like the content, give it some sharing 🙂

Happy Coding 😀
For more Salesforce Content, Please Like & Subscribe to My Facebook Page and Youtube Channel with the links below.
Thank you, See you in the next one & Peace 😀


Stay update with our latest posts by subscribing to our newsletter. It is free!




4 thoughts on “Normalize Text In Salesforce Using Apex”

  1. Hi Raghavendra,
    I have a question for you. Can the font of the fields be changed for a page created on an Lightning App Builder?

  2. Hi Raghavendra,

    I have a question for you. Can the font of the fields changed on the page created with Lightning App Builder?

Leave a Reply

Your email address will not be published.