RIGHT() Function in Salesforce [Formula Syntax & Examples]

While working as a Salesforce Administrator, I created a custom field to store the Account Number in the format ACC-IND-000123.

Now, the sales team has requested a new field that displays only the last 6 digits (000123) of the account number for reporting and reference purposes.

To meet this requirement, I created a formula field and used the RIGHT function in Salesforce. The RIGHT function extracts a specified number of characters from the right side of a text value.

In this article, we will learn about the RIGHT() Function in Salesforce. We will also learn the syntax and real-world scenarios where we can use the Salesforce right function.

What is the RIGHT() Function in Salesforce?

This Salesforce RIGHT() function returns the specified number of characters from the end of a text string. In simple terms, you can say that the RIGHT() function extracts the number of characters specified from the right side of the string.

You can use this function when some important information that you require is on the right side of the string. Some popular uses of the RIGHT() function include extracting product codes, last names, email domains, campaign codes, etc.

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

Furthermore, there are some considerations you should keep in mind when using 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 follows:

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.

Use the RIGHT() Function in Salesforce

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

In the Contact object, there is a field for the customer’s email address. The business requirement is to display only the email domain in a separate field.

For example, if the email address is alex@gmail.com. The new field should display gmail.com.

  1. Go to the Gear Icon. Click on the Setup. Select the Object Manager tab. Select the Object on which you want to create the Formula Field. Here, I’m going to create a Contact Object.
Salesforce RIGHT Function Lightning Example
  1. Select the Formula option from the data type and click Next.
RIGHT Function in Salesforce
  1. Next, provide the Field Label and API Name for the formula field. Then, in the Formula Return Type, select TEXT, and click the Next button.
RIGHT Function in Salesforce Example
  1. Below is the formula for extracting the domain name from a 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 returns the length of an email address.
  • The FIND() function searches for the position of the @ symbol in an email address and returns the result as 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.
  1. 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“, “Help Text“, and determine how to handle empty fields. Then click the “Next” button to proceed to the next step.
RIGHT() Function in Salesforce
  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.
RIGHT Function in Salesforce Lightning Example
  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.
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 see the “Email Domain” field, which shows the result of the RIGHT function.

Let’s understand with an example:

Imagine you have created a contact with the email address “john@gmail.com” and saved it. So, for the email “john@gamil.com”, the formula’s output 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

In this way, we can use the RIGHT() function in Salesforce to extract a specific number of characters from the end of a text field, such as the email domain from a customer’s email address, for reporting and analysis.

Conclusion

I hope you have got an idea about the RIGHT() function in Salesforce. In this, I have explained the syntax and real-world scenarios where we can use the Salesforce right function.

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.