Convert String to Lowercase and Uppercase in Salesforce Apex

Our Salesforce organization recently implemented a customization for handling customer data. The customer service team needs a standardized format for customer names and emails in their records.

The customer service representatives often enter names and emails in mixed cases, leading to inconsistencies. The team wants to enforce a standardized format to avoid duplicates and errors during data processing.

To solve this, I used Apex methods to convert the Name to title case and the Email to lowercase. In the steps below, I will explain how to convert the string to lowercase and uppercase in Salesforce Apex.

Convert String to Lowercase and Uppercase In Salesforce Apex

To convert a String to lowercase, we use the built-in toLowerCase() method, and to convert a string to uppercase, we use the toUpperCase() method.

Convert String to Lowercase in Salesforce Apex

I will explain the string-to-lowercase conversion in Apex in the steps below. Now, navigate to the Salesforce developer console and follow the steps below.

  1. To open the anonymous execution window, click on the Debug > Open Execute Debug Window or press the shortcut keys “Ctrl+E.”
  1. Enter the code below in the anonymous window, select the checkbox, open it in Debug, and click the Execute button.
String name = 'Eva Adams';
System.debug('string in lowercase -' + name.toLowercase());
  1. In the console log window, select debug only. The values will be converted to lowercase.
Convert String to Lowercase in Salesforce Apex

This way, we can convert the String to lowercase in Salesforce Apex by following the above steps.

Convert String to Uppercase in Salesforce Apex

To convert a string to Uppercase, we will use the apex function ToUppercase(). Now, navigate to the Salesforce developer console and follow the steps below.

  1. Open the anonymous window in the Salesforce developer console with the shortcut keys “Ctrl+E.”
  1. Enter the code below in the anonymous window, select the checkbox, open it in Debug, and click the Execute button.
String name = 'Henry Smith';
System.debug('String in Uppercase -' + name.toUppercase());
  1. In the Console log window, select debug only. The values will be converted to uppercase.
Convert String to Uppercase in Salesforce Apex

In this way, we can convert the String to Uppercase in Salesforce Apex by following the above steps.

Convert List of Strings to Uppercase In Salesforce Apex

When you have to convert a list of strings to uppercase or lowercase, there is a different approach, which I have explained in the steps below.

  1. Open the Salesforce developer console and execute the anonymous window with the shortcut keys “Ctrl+E.
  1. Enter the code below in the anonymous window, then select the checkbox Debug only and click the Execute button.
List<String> names = new List<String>{'john', 'henry', 'jeff'};

List<String> upperCaseNames = new List<String>();
for (String name : names) {
    upperCaseNames.add(name.toUpperCase());
}

System.debug ('Uppercase Names: ' + upperCaseNames);

In the above code, we have a list of strings. Then, a new, empty list named upperCaseNames is created to store the names converted to uppercase.

Within the For loop name.toUpperCase() converts the current name to uppercase. After this upperCaseNames.add(name.toUpperCase()) adds the uppercase version of the name to the upperCaseNames list.

  1. In the Console log window, select the checkbox Debug only. After this, you will see a list of strings converted to UpperCase.
Convert List string to Uppercase in Salesforce apex

By following the above code, you can convert the string to UpperCase in Salesforce Apex.

Conclusion

By now, you might have understood the method of string to uppercase and lowercase conversion in Salesforce Apex. In the above methods, we have learned to convert a single string to uppercase and lowercase. We also learned to convert a list of strings to uppercase and the exact steps that should be followed to convert a string to lowercase.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.