You need to sign in to do that
Don't have an account?
Bogdan 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();
}
}
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();
}
}
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
Your trigger is perfect and it should work in both cases either newly creating/updating pba__Property__c record.
Thanks & Regards,
Sakthivel Madesh
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.
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
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.
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
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.
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