Salesforce RIGHT Function [How to Use With Example]

In this Salesforce Tutorial, we will learn about Salesforce Right Function. We will also learn the syntax and real-world scenarios where we can use the Salesforce Right Function.

As a Salesforce developer, I got a task from a client where the client wanted to separate the file extension from the file name so he could efficiently handle the files. I searched for the solution and found that in Salesforce we have a function named “RIGHT()” that helps me to separate out the file extension from the file name.

If you want to learn how I used the Salesforce RIGHT function with many more real-world business examples, and also want to learn how to use the RIGHT function in Salesforce Lightning and Salesforce Classic, go through the complete article.

What is RIGHT Function in Salesforce

The Salesforce RIGHT() Function is one of the TEXT functions of Salesforce, that is used to perform the text operations on the Salesforce fields.

This Salesforce RIGHT() Function, is used to return the specified number of characters from the end of a text string. In simple terms, you can say that the RIGHT() Function extract the number of character specified from the right side of the string.

You can use this function when some important information that you require is on the right end side of the string. Some popular use cases of the RIGHT() function are extracting product code, last name, email domain, campaign code, etc.

In addition, you can say that the RIGHT() function is used while extracting substring, splitting the string, and retrieving the specific suffix codes from the string.

Furthermore, there are some considerations that you must remember while working with the RIGHT() function in Salesforce.

  • If the number of specified characters, in the Salesforce Right function is less than zero, the value gets replaced with zero.
  • You can use the auto-number fields as text fields in Salesforce RIGHT function formulas.

The syntax of the Salesforce RIGHT function is as given below:

RIGHT(text, num_chars)

Here is a detailed description of the parameters:

  • text: The text parameter specifies the text string from which you want to extract the substring.
  • num_chars: The num_chars parameter specifies the total number of characters that you want to extract from the end of the text string.

Let’s see an example of the RIGHT function to understand the function better:

Assume a scenario: you are working in a USA-based digital marketing agency as a project manager. The company gave you the task of creating an automated system, that extracts the file extension from the file name and categorizes the files in different folders based on the extensions.

The company uses the Salesforce tool, so to complete this task I found out and decided to the Salesforce RIGHT() function. Below, is the automatic formula logic for the scenario:

RIGHT(File_Name__c, LEN(File_Name__c) - FIND(".", File_Name__c)

We have learned about the Salesforce RIGHT function with syntax and some considerations. In addition, we have seen a small example related to the function for a deep understanding of the topic.

Let’s move ahead and learn to use the RIGHT() Function in Salesforce Lightning and Salesforce Classic.

Read Salesforce TRIM Function

How to use RIGHT Function in Salesforce Lightning

The following are the steps to use the Salesforce RIGHT Function to return the specified number of characters from the end of a text string in Salesforce Lightning. Let’s take a scenario and understand it better:

Suppose you are working in a company that uses Salesforce as a customer relationship management tool. As a team lead you got a task to distribute the contacts to different team members based on the email domains of the employee.

For this, you need to analyze the email domains of each of the employees and you decided to do this using the Salesforce RIGHT() function. Let’s see the steps on how to use the RIGHT function in Salesforce Lightning to get the solution of the defined scenario.

Step 1: Log in to your Salesforce account in the Lightning Experience interface. If you are not on the primary home setup page within the Lightning Experience, find and select the “Gear” icon at the upper right side. Then, from the dropdown menu, choose the “Setup” (Setup for the current app) option.

Salesforce RIGHT Function

Step 2: Once directed to the setup for the current app, you will see an “Object Manager” option available on the navigation bar; click on it.

Salesforce RIGHT Function Example

Step 3: Now, within the object manager page, a list of both the custom and standard salesforce objects will be displayed. Utilize the “Quick Find Search” to find the specific object on which you want to use the RIGHT function. According to the current scenario, we need the “Contact” object, so search for the contact object and click on it.

Salesforce RIGHT Function Lightning

Step 4: In this step, you will see the “Fields & Relationships” option located at the left side of the page, click on this option. Now, your objective is to create a new field for the purpose of using the RIGHT function, click the “New” button situated at the top right.

Salesforce RIGHT Function Lightning Example

Step 5: After clicking the new button, a list of various field types available in Salesforce will be presented. From the list, choose the field type that you require according to your scenario.

Your main objective is to use the RIGHT function, so you need to opt for the “Formula” field type by clicking on the radio button in front of the formula. Then, click the “Next” button to move to the next step.

RIGHT Function in Salesforce

Step 6: In this step, start by entering the “Field Label” and “Field Name”. In accordance with the scenario, I entered the field label “Email Domain”, whereby the field name was automatically populated upon clicking on the field name box.

Move downwards and select the appropriate data type for the field that will be used to store the resultant output. According to the scenario, the best choice is the text data type, so click the radio button front to the “TEXT” data type, and ensure that the formula return value is formatted as text. Then, click on the “Next” button to move to the next step.

RIGHT Function in Salesforce Example

Step 7: Once you click on “Next”, you will be redirected to the “Add Formula” step. Move your cursor to the right side of the page, where the “Functions Section” is located. Within this section, you will see the “All Functions Categories” dropdown, click on it.

From the dropdown, choose the function category according to the need. Knowing that the RIGHT function falls under the text type functions, select “TEXT” from the available categories.

The list of the functions related to the text category will be shown. Scroll through the list, locate the “RIGHT” function, and select it. After this, click the “Insert Selected Function” button below the function box.

As a result, the function will be inserted into the advance formula tab. Replace the parameters with the appropriate merge field, functions, operators, etc. according to your scenario-specific needs.

Below is the formula for the, given requirement of extracting the domain name of the email from the contact email address.

RIGHT(Email, LEN(Email) - FIND('@', Email))

Here is a detailed explanation of the formula:

  • The RIGHT() function is used to extract the specified number of characters from the right side of the string.
  • The Email is a field containing the contact’s email address whose domain value you want to extract.
  • The LEN() function is used to calculate the length of the email address.
  • The FIND() function is used to search the position of the @ symbol in the email address and return the result in a number of characters.
  • We subtract the result of the FIND() function from the LEN() function to get the number of characters in the domain of the email address.

Step 8: Scroll down and select the “Check Syntax” button. It allows you to validate the formula expression to check whether it contains any errors. Additionally, you have the option to add “Description”, and “Help Text” and determine how to handle empty fields. Then, click on the “Next” button to move to the next step.

RIGHT Function in Salesforce Lightning

Step 9: Once you click on the “Next” button, you will be directed to the “Field Level Security” step. In this step, you can choose which user profiles should be granted edit access to the field through the field-level security settings.

Note that, if you neglect to add the field-level security, the custom formula field we have created will not appear for profiles that have not been selected.

For example, in my case, I selected the checkbox next to the “Visible” option, as I want to make this custom formula field visible for all profiles. After that, click on the next button and move to the next step.

RIGHT Function in Salesforce Lightning Example

Step 10: In this step, choose the specific “Page Layout” where you want to add the custom formula field. The field will be added as the last field on the page layout. If you do not select a layout, the field will not appear on any page. By, default all the page layouts are selected, so I keep it as it is and click on the “Save” button to save the newly created formula field.

How to use Salesforce RIGHT Function in Lightning

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

  • Open the “Contact” item, proceed to create a new contact with the “Email” field, and save the contact.
  • Upon successfully saving the contact, move to the contact details page. Here you will observe the “Email Domain” field, showcasing the outcome derived from the RIGHT function.

Let’s understand with an example:

Imagine you have created a contact, with the email address “john@gmail.com” and save it. So, for the email “john@gamil.com”, the output of the formula is “gmail.com”. This formula effectively extracts the domain name after the @ from various email addresses.

The formula works as follows:

  • LEN(john@gmail.com): 14 number of characters.
  • FIND(@, john@gmail.com): 5th position.
  • LEN(Email) – FIND(‘@’, Email): 9 number of characters. It will return the result as gmail.com
How to use Salesforce RIGHT Function in Lightning Example

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

Read UPPER Function in Salesforce | How to convert String to Uppercase in Salesforce

How to use RIGHT Function in Salesforce Classic

The following are the steps to use the Salesforce RIGHT Function to return the specified number of characters from the end of a text string in Salesforce Classic. Let’s take a scenario and understand it better:

Suppose you are working in an e-commerce company. As a team member, you got a task to track orders and check their current status. A company assigns a unique code to order status that also has the current status of the order at the end.

Your task is to extract the current status of the order from the order status code which is the last two characters. For this, you need to use the Salesforce RIGHT function and extract the current status.

Here is the current status code list that is added to the last of the order status codes that you need to fetch.

Current Status CodeDefinition
PSPending Shipping
PRProcessing
SHShipped
DLDelivered
CNCancelled

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

Step 1: Log in to your Salesforce account in the Salesforce Classic Experience. In case your account opens in the Lightning Experience, navigate to the “Profile”, then from the dropdown menu, click on the “Switch to Salesforce Classic” option.

Salesforce Classic RIGHT Function

Step 2: Once you have successfully logged in to the Salesforce Classic interface, click on the “Setup” option located at the top situated between your username and the help option.

Salesforce Classic RIGHT Function Example

Step 3: Once reach the setup page, scroll down to the “Build” section. Then, click on the “Customize” dropdown and select the specific Salesforce object where you want to use the RIGHT function. According to the mentioned scenario, I searched and clicked on the “Orders” object.

Upon clicking the orders dropdown, you will get a list of the several order options from here select the “Fields” option as our main goal is to create the field where you can use the Salesforce function.

RIGHT Function in Salesforce Classic

Step 4: After selecting the “Fields” option, the page for order fields will be shown. Scroll down to the “Order Custom Fields & Relationships” section and click on the “New” button to start the creation of the new custom field within Salesforce.

RIGHT Function in Salesforce Classic Example

Step 5: In this step, you will get the various field types available in Salesforce. Choose the one that is according to your specific needs. Since your main requirement is to create the formula with function, click on the “Formula” field type by clicking on the radio button adjacent formula option. To proceed to the next step, click on the “Next” button.

How to use RIGHT Function in Salesforce Classic

Step 6: After clicking on the next button you will be reached to the “Choose Output Type”, step. Enter the details in the “Field Label” and “Field Name” fields. In my case, I have entered “Current Order Status” as the field label, and on clicking on the field name box, it automatically populates.

Scroll down to specify the formula return type, for the newly created field. In this case, I have selected “Text” as the data type by clicking on the radio button in front of the text data type. Then, click on the “Next” button to proceed to the next step.

How to use RIGHT Function in Salesforce Classic Example

Step 7: In this step, you are on the enter the formula step. Navigate to the right side of the page, towards the “Functions” section. Click on the dropdown labeled “All Function Categories” and select “Text” As you know the RIGHT function falls under the category of the text functions.

After clicking the text, you will get the list of functions within the text category. Now, find the RIGHT function and select it. After that, click on the “Insert Selected Function” button in the functions section.

The selected function is now inserted into the advance formula box, you can replace the function parameters with the relevant text, fields, etc., as per your requirement.

Below is the formula for the, given requirement of extracting the current status of the order from the order status code.

RIGHT(Order_Code__c, 2)
  • In the formula, we use the RIGHT() function that extracts the rightmost 2 characters from the text stored in the field “Order_Code__c” in Salesforce.

Step 8: Click on the “Check Syntax” button under the advance formula editor to validate your entered formula. If there are no errors, you will get the message “No Synatx errors in merge fields or functions”. Otherwise, an error message will be displayed on the screen if an error is present.

Optionally, enter the “Description” and “Help Text” fields. Additionally, you can specify how to manage an empty field if you wish. Then, click on the “Next” button to proceed to the next step.

How to use RIGHT Function in Salesforce

Step 9: On clicking the next button, you will be directed to the “Field Level Security” page. Choose the profiles you want to grant the field-level security editing privileges. It is important to remember that if no profiles are selected, this specific field will remain hidden from all profiles. To ensure that edit of this field to all profiles, check the checkbox next to “Visible”. After that, click on the “Next” button to move to the next step.

How to use RIGHT Function in Salesforce Example

Step 10: Once clicking the next button the “Add Page Layout” step will shown. Here, choose the appropriate page layout containing the newly created custom formula field. To finally save the recently created field, click the “Save” button.

Use RIGHT Function in Salesforce Classic

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

  • Open the “Order” item, proceed to create a new order with the “Order Code” field, and save the order.
  • Upon successfully saving the order, move to the order details page. Here you will observe the “Current Order Status” field, showcasing the outcome derived from the RIGHT function.

Let’s understand with an example:

Imagine you have created an order, with the order code “ORD12345PS” and saved it. So for the order code “ORD12345PS” the output of the formula is “PS” which means “Pending Shipping”. This formula effectively extracts the current status of the order from the order status code, which is the last two characters.

The formula works as follows:

  • Order Code: It is the unique code of the order with the current delivery status.
  • 2: It specifies that the two characters from the right side of the text string.
Example of RIGHT Function in Salesforce

Conclusion

In a nutshell, we have learned how the Salesforce RIGHT function helps extract the specified number of characters from the end of a text string.

We have learned both the syntax of the RIGHT function in Salesforce and its practical applications. Furthermore, we have observed how effectively the RIGHT function works both within the Salesforce Lightning interface and in the traditional Salesforce Classic environment.

You may like to read the following articles: