LEFT Function in Salesforce

In this Salesforce Tutorial, we will learn how to use the Salesforce LEFT function. Moreover, we will learn the syntax and business use examples of the LEFT function.

While working as a salesforce developer, I got a task from my client to fetch out the plant code from the serial number. To get a solution to this, I searched and found out that Salesforce has one function called “LEFT” that helps me to complete my task.

If you want to learn how to use the Salesforce LEFT function in both Salesforce Lightning and Salesforce Classic with multiple real scenario examples, go through the complete article.

Salesforce LEFT Function

LEFT() Function in Salesforce is one of the available TEXT functions of Salesforce, which is used to perform the text operations on the Salesforce fields. The LEFT() Function in Salesforce, is used to return the specified number of characters from the start of a text string.

In other words, you can say that the LEFT() Function is used to extract the specified number of characters from the leftmost side of the string. You can use this function while extracting substring, splitting the string, and retrieving specific prefix codes from the string.

In addition, the syntax of the Salesforce LEFT function is as given below:

LEFT(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 start of the text string.

Moreover, there are some considerations that you must keep in mind while working with the LEFT function.

  • In the Salesforce LEFT function, if the num_chars parameter is less than zero, the value gets replaced with zero.
  • You can use the auto-number fields as text fields in the Salesforce LEFT function formulas.

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

Suppose there was an e-commerce company that has a high volume of bills every day. The finance department wants to efficiently track and analyze the bills, so the finance team decided that they will organize the bills based on the generated year.

To do this, they use the LEFT function of Salesforce. The format of the bill is YYYYMM-XXX, where the YYYY specifies the year of the generated bill, MM specifies the month of the generated bill, and XXX specifies the unique number.

Here, is the formula to extract the year from the bill number using the Salesforce LEFT function:

LEFT(Invoice_Number__c, 4)

With this, we have learned about the LEFT function of Salesforce with syntax and various important considerations. Moreover, we have seen a small example related to the function for a deep understanding of the topic.

Now, let’s move ahead and learn to use the LEFT() Function in Salesforce Lightning and Salesforce Classic.

Read Salesforce RIGHT Function

How to use the LEFT Function in Salesforce Lightning

The following are the steps to use the LEFT function in the Salesforce Lightning:

Step 1: Login to the Salesforce account lightning experience. If you are not on the main home setup page of Lightning Experience, click on the “Gear” icon at the top right side of the page and then click on the “Setup” option from the dropdown.

LEFT Function in Salesforce

Step 2: Once you are directed on the setup for the current app in Lightning, you will get an option of the “Object Manager” in the navigation bar adjacent to the home tab. Click on it.

LEFT Function in Salesforce Example

Step 3: Now, on the object manager page, you will find the list of all the custom and standard objects of Salesforce. Search for the object in the “Quick Find” search box in which you want to use the LEFT function. Let’s take an example of the “Contacts” object and click on it.

LEFT Function in Salesforce Lightning

Step 4: Once you click on the contact, you will get the option of “Field & Relationship” on the left side of the page section. Click on it, and then click on the “New” button located at the right top.

LEFT Function in Salesforce Lightning Example

Step 5: In this step, you will get the list of all the field types in Salesforce select the one from the list that you need. As our need is to use the LEFT function, for this you need the “Formula” field type.

See also  Salesforce RIGHT Function [How to Use With Example]

In this example: I have selected the “Formula” field type by clicking on the radio button next to the formula field type. Then, click on the “Next” button, to move to the next step.

Salesforce Lightning LEFT Function

Step 6: You are in the “Choose output type” step. Enter the “Field Label” and the “Field Name” will automatically fill up. In this example: I entered the field label as “Area Code”.

Scroll down and choose the data type for the field in which you will want to get the result. As I needed to store the area code, I decided to choose the text data type by clicking on the radio button next to it.

In this example: I select the “Text” field type and set the return value of the formula in the text format. After that, click on the “Next” button to proceed to the next step.

Salesforce Lightning LEFT Function Example

Step 7: Once you click on the next, you will be directed to the add formula page. Move to the right side of the page towards the “Functions Section” and here you will get the “All Function Categories” dropdown, Click on it.

Choose the function categories from the dropdown, as the LEFT function is a text-type function, Select the “TEXT” from the list of the function categories.

It will show you all the functions related to the text category, scroll down and search for the “LEFT” function and click on it, and then click on the “Insert Selected Function” button given below.

The function is inserted into the advance formula tab, Now replace the parameters with the merge field, constant, return value, etc., and create the formula according to your requirement.

Our Requirement: From the phone number that is the length of 12 digits, we want to extract the area code that is the first two digits. Below is the formula according to the above-defined requirement:

LEFT(MobilePhone, 2)

Here, the LEFT() function extracts the 2 characters from the beginning of the “MobilePhone” field and returns the extracted substring as an “Area Code”.

Step 8: To make sure the formula shown above is correct, click the “Check Syntax” button. If the advance formula has no error, you will see the message “No Syntax error in merge fields or function”. If there is an error, you will get the error message.

Optionally, you can define the “Description”, “Help Text”, and “Handle the empty fields”. Then to move to the next step click on the “Next” button.

Salesforce LEFT Function

Step 9: After you click the next, you will directed to the “Establish Field-level Security”. You need to select the profile here to which you want to grant field-level security editing rights for the custom formula field created above.

In this example: I check the “Visible” checkbox and after that click on the “Next” button to move to the next step.

Salesforce LEFT Function Example

Step 10: Now, you are on the “Add to page layouts” step. Select the page layout that has this field in it. Also, take note that this field will be inserted as the last one in the column section of the layout of this page.

Moreover, no pages will have the field if you don’t choose a layout. Then, to save this custom formula field where you used the LEFT function, click on the “Save” button.

How to use LEFT Function in Salesforce

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

  • Open the “Contact” item, create a new contact with the “Mobile” field, and save the contact.
  • Once you save it, move to the details of this contact, and you will see it shows the field “Area Code” according to the result of the LEFT function.

Let’s understand with a scenario:

Suppose, you create a contact with the mobile number “900000000000”. The formula starts executing and fetches out the 2 digits from the leftmost side of the number string i.e. 90.

How to use LEFT Function in Salesforce Example

With this, we have learned how to use the LEFT function in Salesforce Lightning and now we will move ahead and learn to use the LEFT function in Salesforce Classic with an example.

See also  Salesforce TODAY Function

Read Salesforce TRIM Function

How to use the LEFT Function in Salesforce Classic

The following are the steps to use the Salesforce LEFT function in Salesforce Classic:

Step 1: Open your Salesforce account in the Salesforce Classic Experience. If by default your account gets opened in the Lightning Experience, click on “Profile” and then click on the option “Switch to Salesforce Classic” from the dropdown.

LEFT Function in Salesforce Classic

Step 2: After you successfully, logged into the Salesforce Classic click on the “Setup” option at the top between your name and help option.

LEFT Function in Salesforce Classic Example

Step 3: Now, you will be on the main setup page of the classic experience. Scroll down to the “Build” section at the left side of the page and from here click on the “Customize” dropdown.

You will get the list of all the Salesforce Classic objects both standard and custom. Choose the Salesforce object on which you want to use the Salesforce LEFT function.

Let’s take an example of the “Orders” object as I want to identify the type of orders. Click on the “Orders” dropdown, then click on the “Fields” option.

Salesforce Classic LEFT Function

Step 4: After you click on the fields, the contact page has all the custom and standard fields opened. Scroll down to “Orders Custom Fields & Relationship” and click on the “New” button to create a new custom field.

Salesforce Classic LEFT Function Example

Step 5: Now, you are directed to the “Choose the Field Type” step, you have to specify the type of information that the custom field will contain by choosing the “Data Type”.

The main objective is to use the LEFT function and select the “Formula” field type by checking the radio button next to it. After that click on the “Next” button and move to the next step.

How to use LEFT Function in Salesforce Classic

Step 6: Once you click on the next button, you will be on the “Choose Output Type” step. You need to enter the “Field Label” and “Field Name”, then you also need to choose the “Formula Return Type”.

In this example: I entered the field label “Order Type Prefix” and when I clicked on the field name box it automatically filled up. Then, I scroll down and choose “Text” as the formula return type by checking the radio button next to the text. After that click on the “Next” button to move to the next step.

How to use LEFT Function in Salesforce Classic Example

Step 7: In this step, you have to enter the formula. Move to the right side of the page towards the “Functions” sections and from this click on the dropdown named “All Function Categories”. From here choose “Text” As you know the LEFT function is one of the text functions.

After you click on the text you will get all the functions under the text category, search for the “LEFT” function and click on it. Then, click on the “Insert Selected Function” button that will appear under the functions section box.

The function has now been added to the advance formula box and replaced the parameters of the function with text, fields, etc. according to the requirement.

Our Requirement: An e-commerce company logistics team needs to identify the type of order based on the order code to process the high volume of orders.

Each order has a unique order code in the format “ORD-XXXX”, where “ORD” is one type of the order type “Regular Order”, “BULK” is one type of the order type “Bulk Order”, and “PROMO” is one type of the order type “Special Promotion”.

Below is the formula according to the above-defined requirement:

IF(ISBLANK( Order_Number__c ), "", 
    IF(BEGINS( Order_Number__c , "ORD-"),
        LEFT( Order_Number__c , LEN("ORD-")),
        IF(BEGINS( Order_Number__c , "BULK-"),
            LEFT( Order_Number__c , LEN("BULK-")),
            IF(BEGINS( Order_Number__c , "PROMO-"),
                LEFT( Order_Number__c , LEN("PROMO-")),
                "Unknown Type"
            )
        )
    )
)

Here is a detailed explanation of the code:

  • First, use the IF() function, and inside it pass the ISBLANK() function with the “Order_Number__c” field in it. The IF parts check whether the Order_Number__c is empty or not, if it is formula returns an empty string “”, if not proceed toward the second part.
  • In the second part, we use the IF() function, and inside it pass the BEGINS() function with the order number and prefix code to check if the Order_Number__c starts with the prefix “ORD-“. If it is, use the LEFT() function with the LEN() function that extracts the “ORD-“ prefix and returns it, If not proceed toward the third part.
  • In the third part, we use the IF() function, and pass the BEGINS() function to it with the order number and prefix code to check if the Order_Number__c starts with the prefix “BULK-“. If it is, use the LEFT() function with the LEN() function that extracts the “BULK-“ prefix and returns it, If not proceed toward the fourth part.
  • In the fourth part, we use the IF() function, and pass the BEGINS() function to it with the order number and prefix code to check if the Order_Number__c starts with the prefix “PROMO-“. If it is, use the LEFT() function with the LEN() function that extracts the “PROMO-“ prefix and returns it, If not proceed toward the last part.
  • In the last, if none of the above prefixes matches the formula return “Unknown Type”.
See also  CEILING() Function in Salesforce

Step 8: After entering the formula in the advanced formula subtab, click on the “Check Syntax” button to verify whether the above-written formula is correct or not. If there is no error in the formula, you will get the message “No Syntax errors in merge fields or function” Otherwise, you will get an error message.

You can also add “Description”, “Help Text” and “Handle the empty field”. Then click on the “Next” button to proceed to the next step.

Example Of Salesforce LEFT Function

Step 9: In this step, you are on the “Establish Field-Level Security” step. In order to customize field-level security, you must select the profiles to whom you want to provide editing access to the field. This specific field will be hidden from all the profiles if you haven’t selected a profile.

In this example: I have selected the “Visible” checkbox because I want every user on the profiles to have the ability to modify it. Then, click on the “Next” button to move to the next step.

Use LEFT Function in Salesforce Classic

Step 10: Once you click on the next, you will be directed to the “Add to Page Layout” step. Here, you must select the page layout in which the newly created custom formula field will be included. After that, click on the “Save” button to save the formula field.

Use LEFT Function in Salesforce Classic Example

You can use the formula field after you have created it. Let’s look at an example to show how it works:

  • Open the Order item, create a new order with the field “Order Number” and save it.
  • After saving it, move to the Detail Section where you will get the “Order Type Prefix” field with the output of the LEFT function.

Let’s understand this with the example:

Suppose, you create an order with the order number “PROMO-5976”. The formula starts executing and fetches out the characters till the length of the “PROMO-“.

Example of LEFT Function in Salesforce

With this, we have learned how to use the LEFT function in Salesforce Classic.

Conclusion

In conclusion, we have learned the syntax and several actual business-related examples for the Salesforce LEFT function. We have also learned that the LEFT function is used to extract the leftmost side characters of the string.

In addition, we have learned how to use the Salesforce LEFT function in both Salesforce Lightning and Salesforce Classic step by step.

You may like to read the following articles: