Table of contents
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
Go to 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