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
Bogdan PascuBogdan Pascu 

help with a trigger on after insert an object

I have the following trigger on a custom object. Basically it counts the nr of characters from a text filed and display the info in to a numeric filed. The trigger is working as expected when the object is being updated but it fails when you create a new one. 

trigger count_nr_of_characters_on_sales_description_es on pba__Property__c (before insert, before update){ 
for(pba__Property__c a: Trigger.new){ 
System.debug('Number of Characters for Description Field: ' + a.fm_web_Long_description_es__c.length()); 
a.Nr_of_characters_sales_description_es__c = a.fm_web_Long_description_en__c.length(); 

}
Best Answer chosen by Bogdan Pascu
Deepali KulshresthaDeepali Kulshrestha
Hi Bodgan,
I tested your code from my side  and it's working fine for bith before insert and before update case.
Still if you are facing any issues then please attach a screenshot of the error you are getting while creating a new record for your object.
So after seeing your reply I assume that you want trigger to be executed also in case when you are not giving value to text field.
You can use the code provided below to ignore the error.

trigger count_nr_of_characters_on_sales_description_es on pba__Property__c (before insert, before update){ 
for(pba__Property__c a: Trigger.new){ 
  if(a.fm_web_Long_description_en__c == null)
 {
  a.Nr_of_characters_sales_description_es__c =0;
 }
  else{
  a.Nr_of_characters_sales_description_es__c = a.fm_web_Long_description_en__c.length(); 
    } 
}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

All Answers

SAKTHIVEL MSAKTHIVEL M
Hi Bogdan,

Your trigger is perfect and it should work in both cases either newly creating/updating  pba__Property__c record.

Thanks & Regards,
Sakthivel Madesh
Bogdan PascuBogdan Pascu
Hi Sakthivel

The issues is with newly created objects since they don't have any texts I always get an error. For existing ones whne I make chnages it is working. 
Deepali KulshresthaDeepali Kulshrestha
Hi Bodgan,
I tested your code from my side  and it's working fine for bith before insert and before update case.
Still if you are facing any issues then please attach a screenshot of the error you are getting while creating a new record for your object.
So after seeing your reply I assume that you want trigger to be executed also in case when you are not giving value to text field.
You can use the code provided below to ignore the error.

trigger count_nr_of_characters_on_sales_description_es on pba__Property__c (before insert, before update){ 
for(pba__Property__c a: Trigger.new){ 
  if(a.fm_web_Long_description_en__c == null)
 {
  a.Nr_of_characters_sales_description_es__c =0;
 }
  else{
  a.Nr_of_characters_sales_description_es__c = a.fm_web_Long_description_en__c.length(); 
    } 
}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
This was selected as the best answer
Bogdan PascuBogdan Pascu

Hi Deepali

Thank you very much for your help.  The only thing that is not working is when I delete the entire text I get the following error: 

Error:Apex trigger count_nr_of_characters_on_sales_description_es caused an unexpected exception, contact your administrator: count_nr_of_characters_on_sales_description_es: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.count_nr_of_characters_on_sales_description_es: line 8, column 1

Do you think we can overcome that? Kindly appreciate your help. 
Deepali KulshresthaDeepali Kulshrestha
Hi Bodgan,
I don't think you have not tried the code I have provided above as I have tested again and it is working fine in delete case of text too.
Please try this code:

trigger count_nr_of_characters_on_sales_description_es on pba__Property__c (before insert, before update){ 
for(pba__Property__c a: Trigger.new){ 
  if(a.fm_web_Long_description_en__c == null)
 {
  a.Nr_of_characters_sales_description_es__c =0;
 }
  else{
  a.Nr_of_characters_sales_description_es__c = a.fm_web_Long_description_en__c.length(); 
    } 
}
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Bogdan PascuBogdan Pascu
Hi 

I have found the error I was using a diffrent filed _en__c instead of _es__c

Now should be working as expected. I will further test and if all looks good will mark the best answeer. 

Many thanks for your help. 
Deepali KulshresthaDeepali Kulshrestha
Hi Bogdan,

Please let me know if you are still facing any issues. I am happy to help.
And also, please mark the above solution as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha