LPAD() Function in Salesforce[ Syntax, Examples & Use Cases]

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 Salesforce.

Example 1: Creating Readable Entry Codes for Events

To better understand this concept, we will go through a scenario. In an event management company, attendees are divided into two categories: Regular (Reg) and VIP. Each attendee must be given an Entry Code.

The Entry Code should start with the category code (Reg or VIP) and then the Contact ID. To generate this format automatically in Salesforce, we will use the LPAD() function in a formula field.

  1. Go to Object Manager.
    • Click the Gear icon in the top right and select Setup.
    • Click Object Manager and open the Campaign object.
  2. Create a formula field.
    • Go to Fields & Relationships and click New.
    • Select Formula as the field type and click Next.
    • Enter Field Label: Entry Code (Field Name will auto‑populate).
    • Choose Text as the formula’s return type, then click Next.
LPAD Function in Salesforce Lightning
  1. Write the LPAD Formula.
    • On the Enter Formula page, go to the right side and open the Functions panel.
    • Find LPAD in the list and click on it. Then click Insert Selected Function to add it to the formula editor.
    • Now, replace the parameters inside the LPAD function with your actual field and required values based on your scenario.
IF(Category__c = 'VIP', 
   LPAD(Contact_ID__c, 9, 'VIP'), 
   LPAD(Contact_ID__c, 9, 'REG') 
)
  1. First, an IF condition checks whether the Category field contains the value VIP.
    • If Category = VIP, the LPAD() function adds “VIP” to the left of the Contact ID until the total length becomes 9 characters.
    • If Category is not VIP, the LPAD() function adds “REG” to the left of the Contact ID until the total length becomes 9 characters.
    • After entering the formula, click Check Syntax to ensure there are no errors.
    • You can also fill in the Description and Help Text if needed, then click Next.
LPAD Function in Salesforce Lightning Example
  1. Select Field Level Security for the formula field, then select the profiles to make the field visible to them. The field will be visible for the selected profiles in the Visible column.
    • After selecting the profiles, click Next.
Salesforce Lightning LPAD Function
  1. In this step, we must select the field’s visibility on the object’s page layouts. In this case, we have created a formula field for the reports, allowing us to choose not to display it on the page layouts.
    • Uncheck the page layout checkboxes to prevent the field from displaying on those layouts. After this, click on the Save button.
Salesforce Lightning LPAD Function Example
  1. After creating the formula field, you can test how it works.
    • Go to the Contacts object and create a new record. Enter values for Category and Contact ID, then save the record.
    • Open the saved contact record. In the details section, you will see the Entry Code formula field showing the result generated using the LPAD() function.
    • For example, if you create a contact with Category = VIP and Contact ID = 489469, the Entry Code will display VIP489469.
    • This shows that the formula successfully adds the required characters to the left side of the Contact ID.
Example of LPAD Function in Salesforce

In this way, we have learned how to use the LPAD() function in Salesforce.

Example 2: Masking Data for Display

Sometimes you only want to show the last few digits of a number, but still keep a fixed length.

For example, you have a Phone field on an object that stores a 10-digit number. Now, you want to mask the first 5 digits and display only the last 5 digits, like XXXXX25488.

LPAD( RIGHT(Phone, 5), 10, 'X')
  • RIGHT(Phone, 5)
    • Takes the last 5 digits from the Phone field.
    • Example: 9876525488 → 25488
  • LPAD( … , 10, “X”)
    • Makes the total length 10 characters by adding “X” to the left side.
  • Final Result
    • If Phone = 9876525488
    • Output = XXXXX25488
LPAD() Function in Salesforce

This formula masks the first 5 digits and shows only the last 5 digits.

Example 3: Standardizing Numeric Account Numbers

Imagine account numbers come from an external system as raw numbers with different lengths. You want every account number to show as an 8‑digit code with leading zeros.

LPAD(TEXT(AccountNumber__c), 8, '0')
  • TEXT(AccountNumber__c)
    • Converts the number field into text because LPAD works with text.
  • 8
    • Defines the total length the final value should have.
  • ‘0
    • Adds zeros to the left side until the value becomes 8 characters long.
  • Final Result
    • If the user enters a 4-digit value, for example AccountNumber__c = 4154,
      • The output will be 00004154.
    • If the user enters an 8-digit value, for example AccountNumber__c = 12345678,
      • The output will remain 12345678 (no change).

Conclusion

I hope you have got an idea 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.

In this, I have explained different examples of adding the string before the number, replacing the text, and adding a number to the left of the entered value until it reaches the required length.

You may 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.