RPAD Function in Salesforce

Are you facing a challenge in Salesforce that text strings are of a specific length while maintaining data integrity?

Like you have to generate an invoice number, and you want the invoice number to be of a specific length, maybe 12 characters but you want to start the invoice number from 1 only and you are now thinking about how to fill up the remaining 11 characters automatically.

This is where to add up the specified remaining characters on the right side of the text string the Salesforce RPAD Function comes in use.

In this Salesforce tutorial, you will explore the syntax of the RPAD function in Salesforce and the practical examples of its use cases. In addition, you will go through the steps of utilizing the RPAD function in Salesforce Lightning and Salesforce Classic.

What is Salesforce RPAD Function

“Right Pad” full form of the RPAD Function. The RPAD function is one of the various TEXT functions of Salesforce which is used for performing text operations and effective data manipulation on the Salesforce fields.

The Salesforce RPAD function is used to add characters that you specify to the right side of a text string. This function is useful when you need to make sure that the text field is of a specific length and to meet the desired length you add padding characters to the field.

Syntax of Salesforce RPAD Function

The syntax for the RPAD function in Salesforce is as follows:

RPAD(text, padded_length[, 'pad_string'])

Here is a detailed description of the parameters:

  • text: The text parameter specifies the text string or text field to which you want to insert characters to the right side.
  • padded_length: The padded_length parameter specifies the total number of characters that you want in the text expression or text field including padded text. In other words, it is the total number of characters in the text that will be returned.
  • pad_string: The pad_string parameter specifies the characters or sequence of the characters that you want to add to the right side of the text field or text expression.

Consideration of RPAD Salesforce Function

The following are the considerations of the RPAD function in Salesforce that you must keep in mind while working with the RPAD function:

  • No Character Added: If the original length of the text field or text expression is already equal to the padded length then no padded character is added.
  • Default Blank Value: The pad_string is the optional parameter if you don’t specify this parameter by default it takes the blank as the default value.
  • Truncate Text: If the value in the text field or text expression is longer than the pad_string parameter the text in the text parameter is truncated to the size of the padded_length.
  • Incorrect Parameter: If you set any other data type than the text data type, you will get an error “Incorrect parameter type for function ‘RPAD()’. Expected Text, received Other”.

Example of RPAD Salesforce Function

Let’s take a practical example of the RPAD to understand the working of this Salesforce function:

Imagine you have an Order Code field and you want to make sure that all Order Codes must be 8 characters long, for this, you decided to add the zero to the right in the cases where the order code is less than 8 characters.

RPAD(Order_Code__c, 8, '0')

If the original order code is ‘ORD89’, according to the above formula the RPAD function changes the order code to ‘ORD89000’ to match with the requirement of length to be 8 characters.

With this, you have learned detailed information about the RPAD function in Salesforce. Now you will be able to use RPAD Function in both Salesforce Lightning and Salesforce Classic.

How to use RPAD Function in Salesforce Lightning

When working with Salesforce there may be instances where you need to use the RPAD function.

One case is that suppose you want to standardize the contract numbers to be exactly 10 characters long for data consistency, integration, and easy processing.

This section will guide you through the steps on how to achieve this goal by utilizing the RPAD function in Salesforce. Here are the steps to follow:

1. Login to the Salesforce account in the Lightning Experience. Then to redirect to the setup for the current app, click on the “Gear” icon at the top right side, then click on the “Setup” option from the dropdown.

RPAD Function in Salesforce

2. Click on the “Object Manager” option located at the top navigation bar of the Lightning home setup page.

RPAD Function in Salesforce Example

3. Select the appropriate object from the list of custom and standard objects. You can easily search for the object using the “Quick Find” box and click on it, once it appears in the search result list.

For example, according to the above-defined case, you can use the “Contract” object.

RPAD Function in Salesforce Lightning

4. Click on the “Fields & relationship” option from the left section of the contract setup page. Once redirected to the other page, click on the “New” button given at the top.

RPAD Function in Salesforce Lightning Example

5. Select the appropriate data type from the list of the data types available in Salesforce by clicking on the radio button given in front of it to specify the type of information that the custom field will contain.

For example, in this case, you select the “Formula” data type as you want to use the RPAD function which you can easily use in the formula data type. Then, to proceed to the next step, click on the “Next” button.

Salesforce RPAD Function

6. Enter the appropriate “Field Label” and once you click on the “Field Name” box to enter it, it automatically fills up without typing on the basis of the field label.

For example, according to the scenario, you can enter the field label “Standardized Contract Number”.

Scroll down the formula return type section and select the appropriate field type from the list given by clicking on the radio button adjacent to the required field.

For example, in accordance with the scenario you have to choose the “Text” data type. After that, click on the “Next” button and move to the next step.

Salesforce RPAD Function Example

7. To fulfill your above-required scenario, you need to create the formula in the advance formula sub-tab using various functions that you need by following the steps as given below:

Move your mouse to the “Functions” section, click the “All Functions Categories” dropdown, and choose the appropriate category of the function.

After that, select the appropriate functions that you require from the category and click on the button “Insert Selected Function” given under the functions section.

Then, replace the parameters of the functions with field, text, etc. as per your requirement, and shape them to the final formula.

For example, standardize the contract number to exactly 10 characters and pad the right of the string with the ‘X’ character to make it 10 characters long.

Select the “Text” category from the functions category and then from the list select the “RPAD” function and replace the parameters with the above given formula:

RPAD(Contract_Number__c, 10, 'X')

Here is a detailed description of the above-written formula:

  • RPAD function is used to pad the text string with specified characters to the right side, so it will reach the specified length.
  • The Contract_Number__c field is the text string that you passed to the function for padding.
  • The 10 is the total length that you want to string after the padding.
  • The X is the character that you used for padding. If the original string is less than the length defined, the X character will be padded to the right side.

8. After entering the formula to check whether it is correct or has some errors, click on the “Check Syntax” button and verify.

  • Error Free: You will get the success message “No Syntax error in merge fields or function” if the formula is error-free.
  • Error: You will get the error message as per the error in the formula.

Optionally, you can add the “Description” and “Help Text” that display on detail and edit pages when the user hovers over the info icon next to this field.

In addition, a “Blank Field Handling” option is also available, you can use it if your formula references any number, currency, or percent fields, and specify what happens to the formula output when their values are blank.

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

Salesforce Lightning RPAD Function

9. Select the profile to whom you want to grant the field-level security edit access to the above-created custom formula field. The field will be hidden from all profiles if you do not add it to the field-level security.

By default, some of the profiles are already selected, if you want to check any other profile, click on the check box given next to it. You can also check all the profiles by clicking on the check box given next to the “Visible” option.

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

Salesforce Lightning RPAD Function Example

10. Select the page layouts that should include this field. The field will be added as the last field in the first 2-column section of these page layouts. The field will not appear on any pages if you do not select a layout.

By default, all the page layouts are selected, if you don’t want to add this field on all the page layouts, you can unselect them by clicking on the checkbox.

Then, click on the “Save” button to save the process of the formula field creation with the use of the “RPAD” function.

How to use RPAD Function in Salesforce Lightning

11. Once the formula field is created, you will be able to use this formula field. Let’s see an example:

  • Open the “Contracts” item, create a new contract with the field “Contract Number” and save the contract.
  • After saving it, move to the “Detail Section” where you will see the “Standardize Contract Number” field with the output of the RPAD function.

Let’s understand it with the example:

Suppose, you have entered the contract number “CON-4856” and saved it. After the execution of the formula, you will get the output for the standardized contract number “CON-4856XX”. Hence, you can say that the formula effectively adds the padding character ‘X’ to it.

How to use RPAD Function in Salesforce Lightning Example

With this, you have learned how to use the RPAD function in Salesforce Lightning. Now, you will learn how to use the RPAD function in Salesforce Classic in the next section.

How to use RPAD Function in Salesforce Classic

There will be many scenarios where you will need to use the RPAD function in Salesforce Classic. One such scenario can be:

Suppose you are employed in an organization that uses Salesforce and you got the task of managing the user authentication. For this, you need to make sure that the username meets the requirement of the specific length.

To meet this requirement, you have decided to use the RPAD function that will pad or truncate the username as per the requirement.

Let’s explore the steps of how to use the RPAD in Salesforce Classic:

1. Login to the Salesforce account in the Classic Interface. Then to move to the classic setup, click on the “Setup” option located on the topic.

RPAD Function in Salesforce Classic

2. Scroll down to the “Build” section that is located on the left side, click on the “Customize” dropdown and you will get the list of objects in Salesforce, now choose the object from the list.

For example, according to the above scenario, you can select the “Contact” object.

3. You will get the dropdown list once click on the object, and click on the “Fields” option.

RPAD Function in Salesforce Classic Example

4. Move down to the section labeled “Contact Custom Fields & Relationship” section and click on the “New” button to start the process of creating a new custom field.

Example of RPAD Function in Salesforce

5. Select the data type according to the type of information you want to contain in the custom field by clicking on the radio button given front of the field name.

In accordance with the scenario, you need to choose the “Formula” data type, and then click on the “Next” button.

Salesforce Classic RPAD Function

6. Type the “Field Label” for the new custom formula and the “Field Name” will automatically fill up on the basis of the field label.

According to the scenario, you can enter the field label as “Standardized Username”.

After that, move down to the section named “Formula Return Type” and choose the data type according to your need by clicking on the radio given adjacent to the data type.

For example, you can select the “Text” data type as per the defined scenario. After that, to proceed click on the “Next” button.

Salesforce Classic RPAD Function Example

7. Enter the formula as per the above-defined scenario for this move to the “Functions” section on the right.

Click on the “All Function Categories” dropdown and choose the category in which the required function is mentioned. According to the requirement, select the “Text” category.

Find the Select the function from the category that you need. As per the scenario, select the “RPAD” function.

To insert the function in the formula subtab, click on the “Insert Selected Function” button located below the function section.

Next, replace the parameters with the field, text, number, etc. as per the formula needed.

For example, standardize the username to exactly 8 characters, and if the username is shorter pad the username with the space on the right side, or if the username is longer truncate the extra character from the right side to meet the length requirement.

Below is the formula as per the example:

RPAD(Username__c, 8, ' ')

Here is a detailed description of the above-written formula:

  • The RPAD function is used to pad a string with specified characters on the right side.
  • The Username__c is the field in which you want to add or truncate the specified characters.
  • The 8 is the desired length of the text string after padding or in simple terms it is the fixed length that the username must be of.
  • The ‘ ‘ is the character that you want to pad to the text string. It is the space character that you want to add if the original username is less than 8 characters to the right side of the string.

8. To check whether that formula has some error or not, click on the “Check Syntax” button and verify.

In addition, you can add the “Description” and the “Help Text”. It will help the user to know more about how the formula works.

You can also “Handle the blank field” if your formula references any number, currency, or percent fields, etc.

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

How to use RPAD Function in Salesforce Classic

9. You can customize the field-level security by selecting the profiles to whom you want to grant the edit access to the field. If you do not select any profiles, the field will be hidden from all profiles.

By default, some of the profiles are already selected, if you want to select more or to unselect these profiles, you can do so by clicking on the checkbox given next to them.

For example, if you want to give edit access to all the profiles, you can do it in just one click i.e. by clicking on the “Visible” checkbox.

After that, click on the “Next” button and move to the next step.

How to use RPAD Function in Salesforce Classic Example

10. Choose the page layout where you want to include the field. The field will be added at the last.

Note that the field should not appear on any pages. All page layout names are initially selected; unselect those where the field is not required by clicking on the checkbox.

Click on the “Save” button to finalize the creation of the formula field in Salesforce Classic using the RPAD function.

How to use RPAD Function in Salesforce

11. Now, you will be able to use this formula field. Let’s see an example:

  • Open the “Contacts” item, create a new contact with the field “Username” and save the contact.
  • After saving it, move to the “Details Section” where you will see the “Standardize Username” field with the output of the RPAD function.

Let’s understand it with the example:

Imagine, you have entered the contact named “John Doe” and set its username “SuperLongUsername” and saved it. After the execution of the formula, you will get the output for the standardized username field as “SuperLon” for John Doe.

You can say that the formula effectively truncates the extra characters from the string’s right side to make it of length 8 as the stored username is longer than the 8 characters.

How to use RPAD Function in Salesforce Example

With this, you have learned how to use the RPAD function in Salesforce Classic.

Conclusion

It’s possible to specify the length of the string to maintain data consistency and integrity.

You can use the Salesforce RPAD function for your formula field. If you want to insert characters that you specify to the right side of a text string you can use the Salesforce RPAD function.

Moreover, you can truncate the extracted space or characters from the right side of a text string.

Through this whole article, in short, you have learned that the Salesforce RPAD function is used by Salesforce developers for data manipulation.

In addition, you have to understand the RPAD function syntax and use cases. Furthermore, you have also explored the step-by-step implementation of how to use the RPAD function in Salesforce Lightning and Salesforce Classic.

You may like to read the following articles: