I need a way to find the word count of submitted and edited bios. They are stored in a Rich Text Area and need to be no more than 100 words.

I found this to get me word count, but I cannot figure out how to have a field that can be used there. I was going to make a worflow to copy from those fiels to a text field, but the RTA will almost always have > 255 characters and thus be cut off.

Is there anyway to do this without manually copying to Word and running word count?


You can't reliably use LTA, RTA, or ETA fields (Long, Rich, Encrypted) in formulas, but you can in Apex Code. My solution would be simply:

trigger CheckWorkCount on SObject (before insert, before update) {
    for(SObject record: Trigger.new) {
        Integer count;
        if(record.RTA__c != null && (count = record.RTA__c
            .normalizeSpace() // Don't count duplicate spaces
            .stripHtmlTags() // Remove all HTML
            .replaceAll('\\S','') // Remove all non-spaces
            .length()) > 99) { // More than 99 spaces is more than 100 words
            record.RTA__c.addError('Please limit your input to 100 words or less. You currently have '+count+' words.');

I don't know of a non-code way of doing this, unfortunately.

If you want to store the count in another field, you can do that as well.

| improve this answer | |
  • I have yet to dabble into Apex, but I think I could do this. I'd rather populate a Word Count field with the number of words. That should be simple, right? – Jmrburgard Nov 4 '16 at 19:40
  • 1
    @Jmrburgard Yes, something like record.RTA_Word_Count__c = count;. I wish I had a better (configuration-only) way of doing this, but nothing comes to mind, since anything configuration-based would have the same formula limitations. – sfdcfox Nov 4 '16 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.