How to Truncate String in Salesforce Apex

Recently, I managed an email marketing campaign in our Salesforce org, and the marketing team uploaded email subject lines to Salesforce. However, to ensure consistency and avoid any issues with storage limits or formatting when sending emails, I need to truncate subject lines longer than 80 characters.

In this Salesforce tutorial, you will learn how to truncate a string in Salesforce Apex. I will also explain how to truncate the string to a specific length in Salesforce Apex.

Syntax to Truncate String in Salesforce Apex

In Salesforce Apex, the syntax for truncating a string is to use the substring() method. This method extracts a portion of a string to truncate it to a specific length.

String truncatedString = String.substring(0, desiredLength);

In the above syntax, String is the string to truncate. The desiredLength is the number of characters you want to keep, starting from the left (index 0).

Truncate String in Salesforce Apex

We will use the class method to truncate a string in Salesforce Apex. To do so, navigate to the developer console and follow the steps below.

  1. Create an Apex class in the developer console; select File > New > Apex class.
    • Enter the name of the new class and click OK. Then, enter the code below in the developer console and save it.
public class TruncateString {
    public static String truncateString(String input, Integer maxLength) {
        if (input != null && input.length() > maxLength) {
            return input.substring(0, maxLength);
        }
        return input;
    }
}
  1. If there are no problems or errors in the code, then launch the anonymous window with the shortcut key “Ctrl+E”. Enter the code below in the anonymous window and click the Execute button.
String inputString = 'Truncate this String value in debug';
Integer maxLength = 20;

String result = StringTruncate.truncateString(inputString, maxLength);
System.debug('Truncated String: ' + result);
  1. In the console log window, select the checkbox to debug only; there, you will see the truncated value of the string in the debug.
Truncate a string in Salesforce apex

We can truncate a string in Salesforce Apex by following the above steps.

Truncate a String to a Specific Length in Salesforce Apex

In Salesforce Apex, we can also truncate a string to a specific length by applying conditionals. For example, the string should only be truncated if it is longer than 120 characters.

Now, follow the steps below to truncate a string to a specific length in Salesforce Apex.

  1. In the Salesforce developer console, launch the anonymous window with the shortcut keys “Ctrl+E.” Enter the code below in the anonymous window, then click the Execute button.
String sizeString = 'Salesforce is a powerful cloud-based customer relationship management (CRM) platform designed to help businesses streamline their sales, marketing, and customer service processes.';
if(sizeString.length() > 120){
    // Use substring to retain only the first 120 characters
    sizeString = sizeString.substring(0, 120);
}
System.debug(sizeString);
  1. In the above code, I entered a string value of over 120 characters. In the debug, the string will be truncated to 120 characters.
    • In the Console log window, select the checkbox to debug only, and then you will see that the string is truncated to 120 characters.
Salesforce apex truncate string with specific length

This way, we can truncate a String in Salesforce Apex to a specific length with the help of the above code.

Conclusion

In this Salesforce tutorial, we have a method to truncate a String in Salesforce Apex using the substring() method. We also learned about the syntax that is used to truncate strings in Salesforce Apex. Along with that, we also learned about a truncate method, through which we can truncate a string to a specific length in Salesforce Apex.

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.