LEN() Function in Salesforce | Calculate Length of Text

The client wanted all mobile numbers entered in Salesforce to be exactly 10 digits. They need this validation to ensure accurate communication with their customers.

Any number shorter or longer than 10 digits should trigger an error message and prevent the record from being saved.

As a Salesforce Admin, I used the LEN() function in validation to automatically check the length of the mobile number field and enforce this requirement.

In this article, we will learn how to calculate the length of a text using the LEN() function in Salesforce. We will also cover the syntax and real-world scenarios where we can use the Salesforce LEN function.

What is the LEN() Function in Salesforce?

The Salesforce LEN function is used to return the number of characters in a specified text. In addition to this, you can use this LEN() function in various functions, formulas, workflows, conditional formatting, and process automation.

The syntax of the LEN function is as given below:

LEN(text)

Here, the text parameter is the text string or the field whose length you want to calculate.

Let’s see some use cases of how to use the LEN function in Salesforce:

Suppose you are working in an e-commerce company as a Salesforce developer, and you are handling the Orders object of Salesforce to keep track of orders.

To store the orders, you create a field named order number, but you also want to ensure that all order numbers meet a specific length requirement.

So, to enforce that all order numbers must be exactly 6 characters long, you decided to use the LEN function of Salesforce. The following is the formula for this example:

LEN(Order_Number__c) <> 6

With this, we have learned what the LEN function is, its syntax, and its use case. Now, we will learn how to use the LEN Formula in Salesforce.

How to Use the LEN() Function in Salesforce

The following steps show how to use the LEN() function to determine the length of text in Salesforce. Let’s use a scenario to understand the concept.

  1. Open Salesforce Lightning mode. Next, click the “Gear Icon” in the top right corner of the page. Click Setup from the dropdown menu to continue.
How to use LEN function in Salesforce Classic
  1. The setup page opens when you select the Setup option. Here, the “Object Manager” is found in the Navigation Bar next to the Home Page. Click on it.
LEN Function in Salesforce Example
  1. The object manager page opens when you click it. There are numerous objects seen here. Use the Quick Find search field to locate the item we want to use the LEN() function on.
    • Here, I have selected the Contact object.
LEN Function in Salesforce Lightning
  1. Redirect to Validation Rule in Salesforce.
    • After choosing the item, go to the bottom of the page and click on “Validation Rules” from the menu on the left side of the screen.
    • When you select the validation rules option, the validation rules page opens on the right side of the page. From here, select the “New” option from the right side.
LEN Function in Salesforce Lightning Example
  1. To create a validation rule using the LEN() function, go to the Contact Validation Rule page and define an error condition using a true/false formula.
    • If the formula evaluates to true, Salesforce will cancel the save and display the defined error message. The user can then correct the data and try saving again.
  • In this step, enter a Rule Name and Description based on your requirements. For example, you can name it “Phone Validation”. Make sure to check the Active checkbox to enable the validation rule.
Salesforce LEN Function
  1. In this step, enter the formula expression for the validation rule. For the scenario, we want to ensure that the mobile number is exactly 10 digits.
    • Use the LEN() function from Salesforce to check the length. To do this, open the Functions panel, select the Text category, find LEN, and click Insert Selected Function.
    • Then, replace the parameter with the mobile number field using the Insert Field button, and add the necessary operators from the Insert Operator dropdown.
    • If the formula evaluates to true, the error message you define will be displayed.

According to the above-defined scenario, the formula condition is as given below:

LEN(MobilePhone) <> 10
  • The LEN() function calculates the length of the text or field passed to it. In this formula, we use the <> operator, which means “not equal to,” and compare it with 10, the required number of digits for the Phone field.
  • This means that if the mobile number has fewer than 10 or more than 10 digits, the formula evaluates to true and triggers the error message.
  • If it has exactly 10 digits, the formula evaluates to false and allows saving. After entering the formula, click Check Syntax to ensure there are no errors.
  • Once you see the message “No errors found,” you can proceed to the next step.
LEN() Function in Salesforce
  1. Now, define the Error Message that will appear when the formula evaluates to true. In this scenario, the message will display if the mobile number has 10 or more digits. For example, you can enter: “Mobile number must be exactly 10 digits.”
Invalid Mobile Number. Make sure it should be of 10 digits.
  • Next, choose the location where the error message will appear. You can display it at the top of the page or below a specific field.
  • For example, to show the message below the Mobile field, select the Field option.
  • Note that if there are multiple error messages on the page, Salesforce will display them at the top; otherwise, your message appears next to the field that needs correction.
  1. Finally, click the Save button to successfully save your validation rule in the Contact object in Salesforce.
Salesforce Lightning LEN Function
  1. Once the validation rule is created, you can test it on the Contacts object. Create a new contact and enter a mobile number, then save the record.
    • If the mobile number has exactly 10 digits, the contact saves successfully. If it has fewer or more than 10 digits, the validation rule triggers and displays an error below the Mobile field.
  1. For example, if you enter 000000000000 (12 digits), The error message “Invalid Mobile Number. Make sure it is of 10 digits.” appears.
    • This works because the LEN() function returns the length of the field’s text and compares it to the required length.
Salesforce Lightning LEN Function Example

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

Conclusion

I hope you have an idea of the Salesforce LEN function, its syntax, practical examples, and use cases. We have also learned that the LEN function calculates the length of a text. In addition, we have explored the steps of using the LEN function in Salesforce.

You may like to read the following articles:

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.