function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jay StreetJay Street 

How to calculate number of words in long text area field

I wanted to create a formula field that calculates the number of words (or characters) in a long text area, but I see that Salesforce does not have this capability. But I hear that apex can accomplish this? Sadly, I am not good with apex code, but I'm wondering if someone has any ideas on how to draft this code? Here are the specifics I am working with:

  • Calculate number of words (or characters) of field Testimony__c on Custom Object TargetX_SRMb__Essay__c and display results in field Testimony_Word_Count__c (also on Custom Object TargetX_SRMb__Essay__c).
Best Answer chosen by Jay Street
Gregory Kriadis 8Gregory Kriadis 8
You are correct in that you would need to utilize Apex to accomplish this. This kind of logic would be applied in a trigger and, according to best practices, its trigger handler class.

Here is what the trigger would look like.
trigger EssayTrigger on TargetX_SRMb__Essay__c (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            EssayTriggerHandler.calculateTestimonyLength(Trigger.new);
        }
    }
}
And below is the helper class.
public class EssayTestTriggerHandler {    
    public static void calculateTestimonyLength(List<TargetX_SRMb__Essay__c> essayList) {        
        for(TargetX_SRMb__Essay__c essay : essayList) {
            // This will update the character count of the text field
            essay.Testimony_Char_Count__c = essay.Testimony__c.length();
            
            // This will update the word count field on Essay.
            // However, I do not recommend using this, as it does not properly accomodate new lines, special punctuation, double spaces, etc.
            List<String> testimonyArr = essay.Testimony__c.split(' ');
            essay.Testimony_Word_Count__c = testimonyArr.size();            
        }       
    }    
}
As I noted in that comment in the code, I wouldn't implement the word count part of the code without finding a more mature solution to the word count, especially since you are using a long text field where new lines can be reasonably expected.

All Answers

Gregory Kriadis 8Gregory Kriadis 8
You are correct in that you would need to utilize Apex to accomplish this. This kind of logic would be applied in a trigger and, according to best practices, its trigger handler class.

Here is what the trigger would look like.
trigger EssayTrigger on TargetX_SRMb__Essay__c (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isInsert || Trigger.isUpdate) {
            EssayTriggerHandler.calculateTestimonyLength(Trigger.new);
        }
    }
}
And below is the helper class.
public class EssayTestTriggerHandler {    
    public static void calculateTestimonyLength(List<TargetX_SRMb__Essay__c> essayList) {        
        for(TargetX_SRMb__Essay__c essay : essayList) {
            // This will update the character count of the text field
            essay.Testimony_Char_Count__c = essay.Testimony__c.length();
            
            // This will update the word count field on Essay.
            // However, I do not recommend using this, as it does not properly accomodate new lines, special punctuation, double spaces, etc.
            List<String> testimonyArr = essay.Testimony__c.split(' ');
            essay.Testimony_Word_Count__c = testimonyArr.size();            
        }       
    }    
}
As I noted in that comment in the code, I wouldn't implement the word count part of the code without finding a more mature solution to the word count, especially since you are using a long text field where new lines can be reasonably expected.
This was selected as the best answer
ravi veeramallaravi veeramalla
shareit install (https://shareitdownloadx.com)
shareit apk download (https://shareitdownloadx.com)
shareit downloading (https://shareitdownloadx.com)
I wouldn't implement the word count part of the code without finding a more mature solution to the word count.
Jay StreetJay Street
Gregory, this is very helpful! Thank you for responding. Your qualification about properly accommodating new lines, punctuation, etc. is duly noted. I'll keep that in mind! Thank you again for helping someone as myself who is not so good at apex code!
James SwansonJames Swanson
You can always share your code https://ca.bestessays.com/dissertation-services.php here on my website. I will be happy to see more.
Alexia SparrowAlexia Sparrow
Hello, I can resolve this query because I have a lot of knowledge in coding. Now I am a writer and wrote many papers with the help of https://www.quora.com/Is-Papersowl-legit website and I can write some articles for your academy. Which will helpful for you. If you like my offer then please tell me. I will be waiting for your response.
Halian RonaldoHalian Ronaldo
Thanks for sharing this. It'll really useful for my writing work. Where I badly face these issues while doing my work. Especially writers know this problem how much they have to suffer. last time, I'm writing a Rogerian argument definition essay (https://essays.uk/rogerian-argument-definition-and-introduction/) and the client is very strict with the word count. So, at that point, you need to be careful with word count.
John Kings 3John Kings 3
I think the software should be able to identify errors too to help professional writers make required corrections. It's just but a suggestion. If this goes through, I will have significant impacts in helping essay writing websites (https://www.bestwritingbay.com/essay-writing-service) to write quality dissertation papers (https://www.bestwritingbay.com/best-dissertation-writing-services).