You need to sign in to do that
Don't have an account?
Shuhbam Sinha
How to show error on after Update trigger.
Hello,
I am getting below error if I use addError(); on after trigger.
'System.FinalException: SObject row does not allow errors'. Could anyone please help me in on the same.
I am getting below error if I use addError(); on after trigger.
'System.FinalException: SObject row does not allow errors'. Could anyone please help me in on the same.
The addError() method can only be used in certain contexts in Apex. Specifically, it can only be used before a record is saved to the database. When you try to call addError() in an after trigger, the record has already been saved to the database, so Salesforce throws the System.FinalException with the message 'SObject row does not allow errors'.
Reference -> https://www.avenoir.ai/post/apex-trigger-adderror#:~:text=An%20addError()%20method%20is,an%20error%20message%20into%20account.
If you need to indicate an error after the record has been saved, you can create a custom field on the object to hold an error message and update that field in your after trigger.
Here's an example of how you could update a custom error field on the object: In this example, if the MyCustomField_c field is null, the trigger sets the MyCustomErrorField_c field to an error message. The record is then saved to the database without using addError().
Reference -> https://opfocus.com/blog/5-things-that-could-give-your-custom-salesforce-apex-code-hiccups/
If the above information helps , please mark this as best answer.
Thank you.
To show an error message in an after update trigger in Salesforce, you can use the addError method of the SObject class. Here is an example of how to use it:
trigger MyTrigger on MyObject__c (after update) { for (MyObject__c obj : Trigger.new) { if (obj.Field__c > 10) { obj.Field__c.addError('The Field value cannot be greater than 10'); } } }
In this example, the after update trigger runs on the MyObject__c object. The trigger checks if the Field__c value is greater than 10 for each record being updated. If the condition is met, the addError method is called to add an error message to the Field__c field.
When the trigger runs and encounters an error, it prevents the record from being saved and displays the error message to the user. The user can then correct the issue and try saving the record again.
It's important to note that the addError method can only be used in an after trigger and not in a before trigger, as it needs to have access to the record ID. Also, make sure to test your trigger thoroughly to ensure that it works as expected and does not cause any unintended errors.
When an after trigger fires, the sObject record has already been saved to the database, and any changes made to the record in the trigger will not be saved. Since the record has already been saved, Salesforce does not allow adding errors to the record as it would not have any effect on the transaction.
If you need to prevent the record from being saved due to an error condition, you should use a before trigger instead. In a before trigger, you can add an error to the sObject record to prevent it from being saved to the database.
Alternatively, if you need to perform some post-processing logic after the record has been saved, you can use an after trigger, but you should not add any errors to the sObject record. Instead, you could consider using other means to indicate that an error condition occurred, such as writing a log message or updating a custom error field on the record.
In summary, you cannot add an error to an sObject record in an after trigger. If you need to prevent the record from being saved due to an error condition, use a before trigger instead. If you need to perform post-processing logic after the record has been saved, you should not add any errors to the sObject record.
If you find this answer helpful,Please mark it as the best answer,