[SalesForce] Word Count Formula

I have a text area on a form, and also a forumla field that displays the max number of words allowed to be enetered in the the text area.

I want to use a formula as a validation rule, that says if the word count is higher than the max then display an error.

Looking at the standard functions I can see there is not a word count function.

One thing that I did consider was counting using a regex for Character Space Character (a space normally separates to words).

Has anyone ever attempted a word count formula?

Best Answer

You can combine available functions to come up with the following formula which returns a number being the number of words in the field:

LEN(TRIM( Name ))-LEN(SUBSTITUTE( Name ," ",""))+1

Here we are counting the number of words in the Name field.

A breakdown of how this works:

  1. Get the length of the string removing any spaces at the start or end: LEN(TRIM( Name ))
  2. Get the length of the string without any spaces at all: LEN(SUBSTITUTE( Name ," ",""))
  3. Subtract the length of the string without spaces from the length of the string without spaces at the start or end and add 1: LEN(TRIM( Name ))-LEN(SUBSTITUTE( Name ," ",""))+1
Related Topic