Get Line Count of Long Text Area -Salesforce

SF - how to calculate line break count in long text area field

In this article, I am going to talk about how to get line count of long text area field in Salesforce with a formula and a very simple record trigger flow. From this approach, we can get line break count of long text area field value.

The result is as follow.

Long Text Area Line Count
Long text area line count animation

Intro

Sometimes, in our Salesforce app, we need to get the line count from long text area field to use in somewhere else.
But there is a problem that we can’t use formula for long text area fields. If we want to analyze the value of long text area field in Salesforce, we have to use flow or apex until the time I write this article.

In this article, I will show you how we can get line count of long text area field using JSENCODE feature that exists in the flow system. Through this work around, you can get the idea of how to analyze or manipulate the long text area field value in Salesforce.

Setting Up the Field and Flow

I am going to use the ‘Description’ field, a long text area field of Account object in my app for this demonstration. You can use any long text area field you want.

First, let’s create a field named ‘Description Line Count’ in Account object to store our line count value within the record as follow. As the value of line count is decimal, the data type of the field will be Number(18,0).

As we cannot use formula to calculate the line count of long text area, we have to create a flow for this calculation.
Go to setup/Flows and create new flow.
Choose the record-triggered flow and add the setting at the start step as follow.

The target object in my case in Account and the entry condition will be if the Description is changed or not. The calculation will be performed and set the line count value before the record is saved.

Then we will create a formula for calculating line count as follow.

IF({!$Record.Description}='',0,(LEN(JSENCODE({!$Record.Description})) - LEN(SUBSTITUTE(JSENCODE({!$Record.Description}), "\r\n", ""))) / LEN("\r\n")+1)

The JSENCODE will encode the text of Description field by converting line break into \r\n. And the LEN-LEN(SUBSTITUTE) part will count the number of \r\n from encoded text. IF part is for checking if Description is empty or not.
With this formula we can get the line count of long text area field value.

After created formula, assign the formula value to the field we created previously in the next step of flow.

Then, save and activate the flow.

By this approach, you can get the text line count of long text area field in Salesforce. Check the animation gif at the top of this post to see the result of our setup.

Conclusion

From this approach, I hope you get the idea how to analyze long text area field value in Salesforce flow. There are also other encode settings in flow, like HTMLENCODE, JSINHTMLENCODE, URLENCODE, etc. This encode settings can be used to encode the text for further use in apex, aura components, LWC components, etc. We can also use this encode setting with formula to get line break count of text like our example.

I haven’t covered the line count calculation based on word count (eg. 30 words per line) in this article as it becomes another topic. I’ll put the link here after published about this topic.

Leave a Reply

Your email address will not be published. Required fields are marked *