You need to sign in to do that
Don't have an account?

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?
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
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(); } }
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,
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.