In Salesforce, sometimes a requirement is that a Text or Number field must always be stored in a fixed-length format.
However, users do not always follow this rule while entering data. For example, there is a field called Account Number where users must enter a 6-digit number, but a user enters only 4154 (4 digits).
To solve this problem, we can create a Formula field and use the LPAD() function. This function adds the characters or numbers (defined in the formula) to the left side of the entered value until it reaches the required length.
In this case, Salesforce automatically converts 4154 into 004154 by adding leading zeros.
Additionally, LPAD() is not limited to zeros. We can use it to add or replace any specific character or number on the left side based on business requirements.
In this article, you will learn about the LPAD() function in Salesforce, including its syntax, real-time examples, and how and where to use it in Salesforce formulas in simple and practical ways.
What is the LPAD() Function Salesforce?
LPAD stands for Left Pad. It is a Salesforce formula function that adds characters to the left side of a text string. You continue doing this until the entire string reaches a fixed length.
The LPAD Salesforce function adds a specific number of characters to the left of the text string until it reaches the specified length. In simple terms, it is used to insert characters we specify to the left side of a text string.
In addition, we can use this function to format the data. We can even use this function to add zeros, create IDs, etc. The syntax of the LPAD Function in Salesforce is as given below:
LPAD(text, padded_length, pad_string)
Here is a detailed description of the parameters:
- text: The text parameter specifies the string in which we want to add the characters to the left side. In simple terms, it is a field or expression where we want to insert characters to its left.
- padded_length: The padded_length parameter specifies the total length of the text after padding. In other words, it is the total number of characters in the text that will be returned. If the original length of the text is already of this padded length, no padded character is added.
- pad_string: The pad_string parameter specifies characters or sequences of characters that we want to add to the left side of the text. This is the optional parameter. If you don’t specify the parameter, it uses the default value of an empty string.
Let’s see one example to clearly understand the concept of the LPAD Function in Salesforce:
For example, if you have the number field named Account Number__c with value 4154 but want it to be 6 digits, the LPAD() function can add characters to the left until the total is 6:
LPAD(Account Number__c, 6, "0")
This will return:
004154 // 6 is the total required length, and "0" is the character added to the left to reach that length.
How to use the LPAD() Function in Salesforce
Here are the steps to use the LPAD function to pad a specific number of characters to the left side of the text string in S