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
Brian12345Brian12345 

Using Apex to count # of characters in a long text field

I have a business requirement to build a formula that calculate how complete the data is on a custom record and then return a corresponding image (green, yellow, red traffic lights).  

 

As part of this requirement I need to count the # of characters in a long text field.  Unfortunately, long text fields can not be used as part of a Formula, otherwise I would use len(long_text_field__c) < 100 as part of my equation.

 

Plan B is to use a trigger to count the # of characters in the long_text_field__c and insert into count_of_long_text_field__c.  Only one issue here - apex is a mystery to me.

 

While I am not a programmer, I attempted to find some javascript code online and change it to work in a trigger.  I didn't find anything simple enough that will allow me to make an honest attempt at buildling my first trigger.

 

Can someone help me get started here? 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

I just created a custom field of data type Number and used this code as an example. I used the standard Account object, and long-area-text field Description. I loop through all the incoming data for this Trigger, and set the custom field, Character_Count__c with the String Length of the Account's Description field.

 

 

trigger countChars on Account (before insert, before update) { for(Account a: Trigger.new){ System.debug('Number of Characters for Description Field: ' + a.description.length()); a.Character_Count__c= a.description.length(); } }

 

 

 

All Answers

aalbertaalbert

I just created a custom field of data type Number and used this code as an example. I used the standard Account object, and long-area-text field Description. I loop through all the incoming data for this Trigger, and set the custom field, Character_Count__c with the String Length of the Account's Description field.

 

 

trigger countChars on Account (before insert, before update) { for(Account a: Trigger.new){ System.debug('Number of Characters for Description Field: ' + a.description.length()); a.Character_Count__c= a.description.length(); } }

 

 

 

This was selected as the best answer
Brian12345Brian12345
aalbert - this worked perfectly.  Thank you so much!
S_LieS_Lie

check_RA: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.check_RA: line 4, column 14

 

my trigger is : 

 

trigger check_RA on BNF__c (before insert, before update) {
     for(BNF__c cBNF: Trigger.new){                 
          String temp = cBNF.R_A_Comments__c;
          if(cBNF.R_A_Comments__c.length() >= 255) --> line 4
          {
              cBNF.addError('RA Comments is restricted to 255 characters');
          }

      }

}

appreciated your help.

 

Thanks,

 

danav@qualcomm.comdanav@qualcomm.com

S_Lie -- If you are restricting the characters to 255, why not just use a text area field instead of a text area long field?

Also in text area long, 32K is the max, not the min, you can change the field configuration to limit the characters to 255.

Ingrid Stone 1Ingrid Stone 1
thank you just what I wanted
ERP TeamERP Team
How can I change the code to count only the '@' character in the long text area?