LEN Function in Salesforce | How to calculate length of a text string in Salesforce

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

Once I got a task to check the description length and print the message according to that using the Salesforce tool. After researching, I discovered that we use the LEN function to calculate the length of a text string in Salesforce.

If you want to learn how to use the Salesforce LEN function, you have come to the right place. In this complete tutorial, we will explore how to use the LEN function in Salesforce Lightning and Salesforce Classic with examples.

What is Salesforce LEN function

The Salesforce LEN function is one of the TEXT functions that is used to perform the text operations on the Salesforce fields. It is used to return the number of characters in a specified text string. In addition to this, you can use this LEN function, in various functions, formulas, workflow, 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 is LEN  function, its syntax, and its use case. Now, we will learn how to use the LEN Formula in Salesforce Lightning and Salesforce Classic one by one with an example.

How to use LEN function in Salesforce Lightning

The following are the steps to use the LEN function to determine the length of the text in Salesforce Lightning. Let’s take a scenario and try to understand the concept.

Scenario: Imagine you are working in an international communication company and your organization is using Salesforce for customer relationship management. As a manager you got a task, to provide support and assistance to clients, so for this, you store information about multiple clients in the contact object.

But one day while trying to connect with the clients, you found that the contact number that you have stored is 12 digits, which is not possible, and by searching out you observed that this mistake happened in various stored contacts.

Now your task is to solve this issue, so this problem does not occur again. For this, you decided to put validation on the mobile field, that if someone enters an invalid length of number in the mobile field, they will get an error message.

And to put this validation rule, you decided to use the LEN function of Salesforce. Let’s see the steps of how to use the LEN function in Validation Rules in Salesforce Lightning to get the solution of the defined scenario.

Step-1: Login into Salesforce Lightning Setup

  • Open up your Salesforce account in the Lightning interface.
  • If you are not already on the setup page for the main Lightning interface page. Move your course over the “Gear” icon on the right side of the screen, click it, and choose “Setup” (Setup for the current app) from the dropdown menu.
How to use LEN function in Salesforce Classic

Step-2: Choose Object in Salesforce Lightning

  • Once you are on the main setup page, look for the “Object Manager” option in the navigation bar and click it.
LEN Function in Salesforce Example
  • You have now been taken to the object manager page, where you can view both customized and pre-built Salesforce objects. Use the “Quick Find” search box to look up the object on which you want to use the LEN function.
  • As per the scenario, you need the “Contact” object as an example, so look for it and click on it.
LEN Function in Salesforce Lightning

Step-3: Redirect to Validation Rule in Salesforce Lightning

  • 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
  • You have now been forwarded to the page for the “Contact Validation Rule”. Identify an error condition and a related error message to define a validation rule. The error condition is expressed as a true/false Boolean formula expression. The saving will be canceled and an error message will appear if the formula expression returns true. The user can correct the error and try again.
See also  TRUNC() Function in Salesforce

Step-4: Setup Validation Rule in Salesforce Lightning

Section-1: Validation Rule Edit

  • In this step, enter the “Rule Name” and “Description” according to your requirements. As per the scenario, let’s suppose you enter the rule name as “Phone Validation”. Also, make sure that the “Active” checkbox is checked.
Salesforce LEN Function

Section-2: Error Condition Formula

  • In this section, enter the formula expression. If this formula expression is true, display the text defined in the Error Message area. As per the scenario, you need the validation on the mobile field, to make sure that the mobile number must be of 10 digits only.
  • To check its digit, you need the LEN function of Salesforce. For this, move your course to the left side towards the Functions, and from the “All Function Categories” dropdown choose the category that includes the required function.
  • As you know the LEN function is one of the text functions, Click on the TEXT category, and now from the list of text category functions find the LEN function and select it. After that, click on the “Insert Selected Function” button.
  • After this, the function is successfully inserted in the box, now replace the parameter with the expression as per the validation rule. Enter the field using the “Insert Field” button and the operators using the “Insert Operator” dropdown button.

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

LEN(MobilePhone) <> 10
  • Here is a detailed explanation of the formula:
    • We use the LEN() function of Salesforce that calculates the length of text or field passed to it.
    • Next, we use the <> symbol which means “Not equal To” in logical expression.
    • Then, we specify the value equal to 10, the value that we are comparing the length of the MobilePhone field.
  • The formula works as explained below:
    • It means, if the MobilePhone has less than 10 characters or more than 10 characters, the formula evaluates to true.
    • If the MobilePhone has exactly 10 characters, the formula evaluates to false.
  • After entering the error condition formula, click on the “Check Synatx” button to make sure that the formula has no error. Once you get the success message “No errors found”, move to the next section.
Salesforce LEN Function Example

Section-3: Error Message

  • Now, it’s time to define the “Error Message” in the box. This message will appear when the Error Condition formula is true. As per your scenario, the error message will be displayed if the Mobile field is less than or greater than 10 digits. Let’s suppose, you enter the message as given below:
Invalid Mobile Number. Make sure it should be of 10 digits.
  • After this, “Enter Location”, and choose the location for the error message. This error message can either appear at the top of the page or below a specific field on the page.
  • To select it check the radio button either next to the “Top of Page” or “Field” option. Let’s imagine, you want to display the error message below the field, so I check the radio button in front of the “Field” option.
  • Note down one more thing, if there is more than one error message, it displays the error message at the top of the page. Otherwise, it displays your error message next to the field the user should correct.

Step-5: Simply click on the “Save” button, to successfully save your validation rule in the contact object in Salesforce Lightning.

Salesforce Lightning LEN Function

Step-6: Testing of the Salesforce Lightning Validation Rule & LEN Function

  • Once the validation rule is created, you can use the formula field and check its work on it.
    • Open the “Contacts” object, create a new contact with the field “Mobile” and save the contact.
    • Once you save, if the Mobile field has 10 digits, it successfully saves the contact. But if the Mobile field is less than or greater than 10 digits, it shows you an error below the field.
  • Let’s deeply understand it with the example:
    • Suppose you have created a contact with the mobile number “000000000000” and save it. The validation rule executes and finds that it is 12 digits instead of 10 digits. The output of the formula is “Invalid Mobile Number. Make sure it should be of 10 digits.”. This formula effectively calculates the length of the text in the string.
Salesforce Lightning LEN Function Example

With this, we have learned the use of the LEN function in Salesforce Lightning. Now, we will move and explore the implementation of the LEN function in Salesforce Classic, with the help of a scenario.

Read Salesforce REVERSE Function | How to reverse string in Salesforce

See also  FIND() Function in Salesforce

How to use LEN function in Salesforce Classic

The following are the steps to use the LEN function to determine the length of the text in Salesforce Classic. Let’s take a scenario and try to understand the concept.

Scenario: Let’s imagine you are working as a marketing manager in the organization and as a marketing executive you need to keep track of the product description. To analyze the description, you decided to calculate the number of words in each description, and for this, you decided to use the LEN function. But there is one twist, in the scenario, you need to count the words only including letters, numbers, and punctuation, but not any spaces.

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

Step-1: Sign in to the Salesforce Classic interface. If by default, you get signed into the Lightning experience, move to the classic one by clicking on the “Profile” icon, then click on the “Switch to Salesforce Classic” from the dropdown.

LEN Function in Salesforce Classic

Step-2: After signing into the Salesforce Classic, click on the “Setup” option located at the top right of the page, to redirect to the Salesforce Classic setup.

LEN Function in Salesforce Classic Example

Step-3: Scroll down to the “Build” section located on the left side of the page. Then, to get the list of all objects of Salesforce, click on the “Customize” dropdown and choose the object on which you want to use the LEN function. After that, click on the “Fields” option from the choose object dropdown.

As per the scenario, you need the “Product” object as an example, so look for it and click on it.

How to calculate length of a text string in Salesforce

Step-4: Once you click on the fields option, you will be directed to the page where you will get the list of standard and custom fields. Scroll downward to the end of the page to the “Product Custom Fields & Relationships” section and click on the “New” button to create a new custom field.

Calculate length of a text string in Salesforce

Step-5: In this step, you are on the “Choose the Field Type” page. Specify the type of information that the custom field will contain as per the scenario. As per the scenario, you need to use the LEN function and for this, the requirement is the formula data type. So, click on the radio button in the front of the “Formula” data type. Then, click on the “Next” button.

How to calculate length of a text string in Salesforce Example

Step-6: As soon as you click on the next button, you are on the “Choose Output Type” step. In this step, first, enter the necessary information such as “Field Label” and “Field Name” and then “Choose the formula return type” as per the requirement.

In accordance with the scenario, I entered the “Product Description Word Count” as a field label, and on clicking on the field name it gets automatically filled. Also, I select the “Number” as the formula return type field by clicking on the radio button next to the number. In addition, to this also set its “Decimal Place” to 0, as by default it is 2.

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

Calculate length of a text string in Salesforce Classic

Step-7: After you click the next button, you move to the “Enter Formula” step. Now navigate to the “Functions” section on the right side of the page. To get the categories of the functions in Salesforce that are all available, click on the “All Functions Categories” dropdown.

From the dropdown list choose the category, that will consist of the function that you need. According to the scenario, you need the LEN function which is part of the text category of the functions, so look for the “Text” category and click on it.

Once you choose the category, you will get the list of all the functions of the text type, From here look for the function that you require. Here as per the scenario, you need to search for the “LEN” function, so after you find click on it to select.

Now to enter this function in advance formula subtab, click on the “Insert Selected Function” button given under the function section box. Once the function gets inserted, replace the function parameter with the appropriate text, fields, etc. as per the requirement.

Below is the formula as per the scenario, that counts the words in the description without counting the spaces.

IF(LEN(Description) > 0,
   LEN(Description) - LEN(SUBSTITUTE(Description, ' ', '')) + 1,
   0
)

Here is a detailed explanation of the code:

  • We use the IF statement, which checks if the “Description” field is not empty using the LEN function and > operator.
    • If the description field is empty the formula returns the result as 0.
    • If the description field is not empty the formula works as described below:
      • First, it counts the length of the characters in the description with space using the LEN function.
      • Second, it counts the length of the characters in the description without space using the LEN function with the SUBSTITUTION function, as the SUBSTITUTION function replaces all spaces with nothing ”.
      • Third, it counts the total number of spaces in the characters, by subtracting the length of the text with spaces from the length of the text without spaces.
      • Last, it adds 1 to the above-obtained result to add the last word, which may not be followed by the space, to get the final words in the description without any space.
See also  TEXT() Function in Salesforce

Step-8: Now, it’s time to check whether the formula is correct or not, For this click on the “Check Syntax” button. If the formula is correct, you will get the message “No syntax errors in merge fields or functions”. If the formula has some error, you will get the error message.

In addition, if you want you can enter the “Description” and “Help Text” that will define the formula. Moreover, you can specify what happens to the formula output when any number, currency, or percent fields in your formula have empty values. Either blank fields should be treated as zeroes or as blanks, depending on your preference.

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

Calculate length of a text string in Salesforce Classic Example

Step-9: Once you click on the next button, you will be directed to the “Establish Field-Level Security” step. By using this field-level security, choose the profiles to whom you wish to provide edit access. If the field is not added to field-level security, it will be hidden from all profiles.

Some of the field-level security profiles are already selected by default, however, you can deselect any of the profiles by using the checkbox next to it. As per my requirement, I want to give access to all the profiles, so I simply click on the “Visible” checkbox. After that, click on the “Next” button to move to the next step.

How to use LEN function in Salesforce Classic

Step-10: In this step, you are taken to the “Add to Page Layouts” step. Choose the page layouts that the field should be on. And always keep one thing in mind, the field will be added as the last field in the first 2 column section of these page layouts.

In addition, no pages will include the field, if you have not chosen the page layout. By default, all page layout names are checked, to only display this information on particular page layouts, uncheck the checkbox next to the desired page layout.

To finish the process of establishing the formula field in Salesforce Classic using the LEN function, click the “Save” button.

How to use LEN function in Salesforce Classic Example

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

  • Open the “Products” object, create a new product with the “Description” field, and save it.
  • After saving it, move to the product details page, here you will get the “Product Description Word Count” field showing the result of the LEN function.

Let’s understand with an example in detail:

Suppose, you have created a product with the Product Description “The iPhone 15 is a high-performance device with a sleek design.” Now the formula will execute and show the product description word count as 11. It means that the formula effectively calculates the word in the description with space.

Let’s learn how the calculation works:

  • Length of the string with spaces included: 63 characters
  • Length of the string without spaces included: 53 characters
  • Subtract the length without spaces from the length with spaces: 63-53 10 characters which means there are only 10 spaces between the words in the sentence.
  • As the last word is not followed by the space, to get the accurate result you need to add 1 to it: 10+1 = 11
Example of LEN function in Salesforce

With this, we have learned how to use the LEN function to calculate the length of a text string in Salesforce Classic with an example.

Conclusion

In conclusion, we have learned the Salesforce LEN function, its syntax, its practical examples, and some use cases. We have also learned that the LEN function is used to calculate the length of the text. In addition, we have explored the steps of using the LEN function in Salesforce Lightning and Salesforce Classic.

You may like to read the following articles: