LPAD Function in Salesforce

In this Salesforce tutorial, we will learn the LPAD Function in Salesforce. In addition, we will learn the syntax and some practical scenarios where we can use the LPAD function in Salesforce. Moreover, we will explore the steps of using the LPAD function in both Salesforce Lightning and Salesforce Classic.

What if you are utilizing Salesforce as your robust customer relationship management (CRM) platform and need to add leading spaces or an optional padding string to the left side of a value?

The solution is simple you can use the LPAD Function in Salesforce. So, If you want to learn how to use the LPAD function in both Salesforce Lightning and Salesforce Classic, you have come to the right place.

Let’s dive deep into the article and learn the concept of the LPAD function in Salesforce.

Salesforce LPAD Function

LPAD stands for “Left Pad”. The Salesforce LPAD function is one of the various TEXT functions of Salesforce which is used for performing text operations on Salesforce fields.

The LPAD Salesforce function is used to add a specific number of characters to the left side of the text string till the specified length we want it to be. 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. Even we can 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 words, it is a field or expression in which we want to insert the characters towards its left side.
  • padded_length: The padded_length parameter specifies the total length of the text after padding. In other words, it is the number of total 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 takes the default as blank.

Note: If the value in the text is longer than the pad_string, the text is truncated to the size of the padded_length.

Let’s see one example to clearly understand the concept of the LPAD Function in Salesforce:

Suppose you are working for a manufacturing company and you got a task to generate and assign a code to the product. But your company follows some format for the code generation that is the code is based on the product ID and it must be 8 digits.

But there was a problem in cases when the product ID was only of the 5 digits. So for these cases, the company decided to add leading zeros. As the company is using the famous CRM tool Salesforce, this task is easily accomplished, because in Salesforce we have one function called LPAD that adds the text to the left of the string.

Below is the formula according to the scenario:

LPAD(Product_ID__c, 8, '0')

With this, we have covered the Salesforce LPAD function, with its syntax and practical applications that explain the workings of the LPAD function in detail. Next, we will explore step-by-step how to use the LPAD function in both Salesforce Lightning and Salesforce Classic, with an example.

How to use LPAD Function in Salesforce Lightning

Here are the steps to use the LPAD function to pad the specific number of characters to the left side of the text string in Salesforce Lightning. To understand this concept better, we will go through a scenario.

Scenario: Suppose you are working in an event management company and one day you get the task of arranging an event for an organization. The organization gives you instructions to do the setting arrangement and it should be divided into two categories Regular and VIP having codes as “Reg” and “VIP”.

According to the setting arrangement you have to give contacts an entry code. So your organization decided to add the category code on the left side and then add the contact ID of the attendees.

So for this, as a Salesforce developer, you decided to use the LPAD function. If you want to learn how to use the LPAD function in Salesforce Lightning follow the below given steps:

See also  COS() Function in Salesforce | Calculate cosine value in Salesforce

1- Login to your Salesforce Lightning interface by entering your username and password. Check whether you are on the main setup page of the Lightning interface, if not move your cursor to the “Gear Icon” and click on it, and then from the dropdown menu, select “Setup” (Setup for the current app).

LPAD Function in Salesforce

2- Now from the current app page, navigate to the “Obejct Manager” option at the navigation bar and click on it.

LPAD Function in Salesforce Example

3- After you click on it the object manager page will be opened. Here you will get the Salesforce custom and standard objects, from these choose any one object on which you want to use the LPAD function. As per the scenario, I search for the “Contact” object in the “Quick Find” box, and once it appears on the search result, click on it.

Salesforce LPAD Function

4- In this step, you are directed to the contact page, now on the left side of the page, locate and select the “Field & Relationship” option. Once the field and relationship page opens, click on “New” located at the top as your need is to create a new field that stores the formula.

Salesforce LPAD Function Example

5- Upon clicking on the new button, you will get the step “Salesforce Field Types”. Select the one data type from the list of the data types as per your specific needs. In this case, according to the scenario, I choose the “Formula” data type by clicking on the radio button next to it as your need is to create the formula field where you can use the LPAD function.

Then, click on the “Next” button to move to the next step.

LPAD Function Example in Salesforce Lightning

6- In the “Formula Return Type” step, enter the “Field Label” and “Field Name”. According to the situation, I entered the field label as “Entry Code” and when you click the field name it gets automatically filled up.

Then, choose the formula return type from the list of available field types by scrolling down to the page. According to the scenario, you need to choose “Text” as the formula return, and for this, you need to check the checkbox next to the text type. After that, click on the “Next” button to proceed to the next step.

LPAD Function in Salesforce Lightning

7- In this step, you are on the “Enter the Formula” page. Move to the right side of the page in the “Functions” section. From here you will see the “All Functions Categories” dropdown, click on it. Once you click you will get the multiple functions categories that are available in Salesforce. As you know the LPAD function is part of the text function, choose the “TEXT” category from the list of function categories.

Once you click on it, you will get all the functions related to the text category, and your main agenda is you find the “LPAD” function, so search it and click on it. After you click on the function, you will get the option of the “Insert Selected Function” button under the function category box, click it, so that the function gets inserted into the advanced formula subtab.

Replace the parameters with the appropriate merge field, functions, operators, etc. according to your scenario-specific needs. Below is the formula according to:

IF(Category__c = 'VIP', 
    LPAD(Contact_ID__c, 9, 'VIP'), 
    LPAD(Contact_ID__c, 9, 'REG')
)

Here is a detailed explanation of the formula:

  • First, we use the IF statement that checks whether the value of the “Category” field is VIP or not.
  • If the value is equal to the VIP then use the LPAD function that left pad the VIP character to the “Contact ID” field to a total length of the 9 characters.
  • If the value is not equal to the VIP then use the LPAD function that left pad the REG character to the “Contact ID” field to a total length of the 9 characters.

8- Click the “Check Syntax” button after entering the formula to verify it and check for any syntax errors. You also have the option of including “Description” and “Help Text” and specifying what to do with empty fields. After that click on the “Next” button.

LPAD Function in Salesforce Lightning Example

9- Now you are on the page, where you need to setup the “Field Level Security”. The field-level security settings allow you to specify which user profiles should have access to edit the field. If you don’t add the custom formula field to the field-level security the field will not appear for profiles that have not been selected.

Since I want to make the custom formula field available for all profiles, I have checked the box next to the “Visible” checkbox. After that to proceed to the next step, click on the “Next” button.

Salesforce Lightning LPAD Function

10- In this step, you are on the “Add to Page Layouts” page. Select the page layout that will include the newly created custom formula type field. By default, all the page layouts are selected, but if you do not want to include the field in the specific page layout uncheck it. To finally save it, click on the “Save” button.

Salesforce Lightning LPAD Function Example

Once the formula field is created, you can use the formula field. Let’s see an example:

  • Open the “Contacts” object, create a new contact with the “Category” and “Contact ID” and save it.
  • After saving it, move to the contacts details section, here you will see the “Entry Code” field that shows you the result of the LPAD function.
See also  PI() Function in Salesforce

Let’s deeply understand this example:

Imagine you have created a contact, with the category “VIP” and contact ID “489469” and save it. After the execution of the formula you get the output for entry code as “VIP489469”. So, you can say that the formula effectively pads the characters at the left side of the string text.

Example of LPAD Function in Salesforce

With this, we have learned the utilization of the LPAD  function in Salesforce Lightning. We shall now move and learn the steps of implementation of the LPAD function in Salesforce Classic, with the help of an example.

How to use LPAD Function in Salesforce Classic

The following are the steps that are used to pad the specific characters to the string using the LPAD function in Salesforce Classic.  Let’s take a scenario and understand it better:

Scenario: Suppose you are working in an organization that is dealing with financial work, and you got a tasked to manage the customer account numbers. To standardize the account number to ensure data consistency, you decided to add the leading zeros to the account number.

Previously the account numbers were stored in numeric values, with different lengths. To standardize the account number, your organization decides that the account number has a fixed length of 8 digits, and if the account number is not 8 digits, you will add the leading zeros to it.

Let’s see the steps on how to use the LPAD function in Salesforce Classic to get the solution to the defined scenario.

1- Signed to Salesforce account in the Classic Experience. But by default, your account opens in the Lightning Experience, go to “Profile” and select “Switch to Salesforce Classic” from the dropdown menu.

LPAD Function in Salesforce Classic

2- After that, click on the “Setup” option, at the top.

LPAD Function in Salesforce Classic Example

3- Once you reach the main setup page of the classic interface, scroll to the “Build” section, click on the “Customize” dropdown, and from the dropdown menu choose any one object on which you want to utilize the LPAD function. As per the above-defined scenario, I searched for “Accounts” and after finding it, clicked on it.

After that, you will get multiple options. So as you want to create a new custom field for entering the formula, click on the “Fields” option.

Salesforce Classic LPAD Function

4- Upon clicking on the fields option, you are redirected to the fields and relationship page. Now, scroll down the page and move to the section named “Account Custom Fields & Relationships” and from here click on the “New” button.

Salesforce Classic LPAD Function Example

5- In the “Choose the field Type” step, you have to specify the type of information that the custom field will contain by checking the radio button given next to the data type that you need. According to the scenario, you need to check the radio button next to the “Formula” as you need it for entering the formula with the function. Then, click on the “Next” button and move.

How to use LPAD Function in Salesforce

6- In this step, you need to enter the “Field Label” and “Field Name” for the newly created custom field. As per the scenario, you can enter the field label as “Standardize Account Number” and when you hover your mouse over the field name, it immediately fills with data.

Next, scroll down the page and choose the “Formula Return Type” from the list of the data type. In accordance with the scenario, you need to choose “Text” as the return type by clicking on the radio button next to it.

Then, to proceed to the next step, click on the “Next” button.

How to use LPAD Function in Salesforce Example

7- This is the main step, where you enter the formula with the function LPAD. For this first, navigate to the right side of the page towards the “Functions” section. Now from the “All Functions Categories” dropdown choose the function category and then from the function category choose the function that you need.

See also  MOD() Function in Salesforce | Calculate remainder in Salesforce

According to the scenario, you need the “LPAD” function which falls under the “TEXT” category, so click on it and after that, you will get the button “Insert Selected Function” button given under the box. Once the function gets inserted into the advance formula subtab, replace the function parameter with the appropriate text, fields, etc. as per the requirement.

As per the scenario, below is the formula with the use of the LPAD function:

LPAD(AccountNumber, 8, '0')

Here is a detailed explanation of the formula:

  • LPAD function is used to pad the character to the left side of the text string until it reaches the specified length.
  • The Account Number is the field that specifies the text string in which you want to pad the sequence of characters.
  • 8 is the desired length of the resulting string. By using the LPAD function you will pad the original string with the character on the left side until it reaches the total length of 8 characters.
  • 0 is the character used for the padding. By using the LPAD function you will add the 0 to the left of the account number field till the total length becomes 8 characters.

8- After entering the formula, click on the “Check Syntax” button located under the formula box to check whether the formula has any error. If there are no errors, you will get the message “No Syntax errors in merge fields or functions”. If there is an error you will get an error message.

You can also add the “Description” and “Help Text”. Even if you want you can handle the blank field. After that, click on the “Next” button to proceed to the next.

How to use LPAD Function in Salesforce Classic

9- In this “Establish Field-Level Security” step, you need to choose the profiles to whom you want to grant field-level security access to editing fields. If you don’t select any profile, the field will be hidden from these specific profiles.

Some of the profiles are already selected, if you want to select reset of the profiles you can do so by checking the checkbox next to them. But if you want to give access to all the profiles, simply check the checkbox next to the “Visible” option. Then click on the “Next” button to move.

How to use LPAD Function in Salesforce Classic Example

10- Select the page layouts that will include the newly created custom formula type field. By default, all the page layouts are selected If you don’t want to include the field in the specific page layout, uncheck them and then click on the “Save” button.

Use LPAD Function in Salesforce

11- Once the formula field is created, you can use the formula field. Let’s see an example:

  • Open the “Account” object, create a new account with the field “Account Number” and save it.
  • After saving the account details, move to the account details page and you will see the “Standardized Account Number” field showing the result of the LPAD function.

Let’s understand with an example:

Suppose, you have created a new account with the account number “429793” and saved it. For the standardized account number, the output of the formula is “00429793”. Hence, you can say that the formula effectively pads the zero to the account number to make it 8 digits using the LPAD function.

But if the account number field has the value “98745614” the result would be the same “98745614” because the length of the account number is already 8 therefore no padding is needed.

Use LPAD Function in Salesforce Example

With this, we have learned how to use the LPAD function to pad the left side of the value with spaces or the optional pad string so that the length is padded length.

Conclusion

In conclusion, we have learned that the Salesforce LPAD function is one of the functions used for formatting and manipulating text. Mainly the LPAD function is used to add or pad the leading characters to the text string.

In addition, we have also learned the syntax and its practical applications. Moreover, we have explored the steps of utilizing the LPAD function in Salesforce Lightning as well as in Salesforce Classic.

You may like to read the following articles: