+ Start a Discussion

Any alternative of ApexPages.hasMessages on Trigger / Apex Class level


I am facing a problem in my trigger class. I am using Trigger Helper class for all my business logic. Here in this example, my "businessRule1" method validate the record, add error for invalid case then perform the business logic. The "businessRule2" is independent from "businessRule1" and should call only if record is not containing any error.

One way is to implement this is "businessRule1" should return some flag "result" that every thing is OK or not.

Here I just want to know :

1. Do we have any other alternative way for this?
2. Any helper method exists like.. "hasError", "isError" on object (i checked but didn't find anything related to this on SObject)
3. What if I want to get all error messages here?
4. We can use "ApexPages" helper methods in our custom controller class but what about normal Apex classes or Triggers?

trigger MyTrigger on Test__c (before update) {
    boolean hasError = false;
    // hasError = ApexPages.hasMessages();
    if(!hasError) {

public class TestHelper{
    public static void businessRule1(List<Test__c> newList){
        if(true){ // failed in some business condition
            newList.get(0).addError('Test Error');
//          newList.get(0).name.addError('Test Error on field');
    public static void businessRule2(List<Test__c> newList){
        // perform some other business logic here

 Thanks in advance.


1. You could use utility class to track the progress of the records and capture additional information about error counts etc.

2. Not that I'm aware of.  Effectively you are marking the record for the benefit of the database and UI, not your code.

3. I don't think you can get at the error messages, so you'd have to have track this yourself

4. You can use apex classes in a trigger.  You can't use triggers in triggers, although they will be used by the platform if necessary.


Hi Bob,


Thanks for quick response.


Here my only concern is that some how we can check that the SObject is contaning any error or not. As SFDC don't provide any utitlity method for this, do you have any idea, what exactly happen behind the screen when we call "addError" method.

They may add this error message in any field or in some related list and if that field /retaled list is open to access for us then I may create utility method on that.


Else I have to go with either my solution ("result" flag) or your solution #1, where I will create some utlity class to track the error count and so..


One more time, Thanks a lot for all your effort.


I don't know exactly how it works, but adding errors to fields/objects will result in error messages being displayed to the user.  I still don't think you can get at any of this information reliably, outside of some of the page level stuff via the ApexPages.hasMessages.


Take a look at the Database class in the Apex documentation, it has plenty of ways of checking if your delete / upsert etc. has worked OK.




Do something like this:



trigger MyTrigger on Test__c (before update) {
	for(Test__c t: trigger.new) {
		if(!businessRule1(t)) {
			t.addError('Failed business rule 1');
		} else if(!businessRule2(t)) {
			t.addError('Failed business rule 2');

public class BusinessRules {
	public static boolean businessRule1(Test__c t) {
		//business rule 1 logic
		return businessRuleOk;

	public static boolean businessRule2(Test__c t) {
		//business rule 2 logic
		return businessRuleOk;



@ColinKenworthy2 - thing is, the database action hasn't completed yet.  This is attempting to determine inside a trigger if there were any errors earlier in this or other triggers, to decide whether its worth proceeding with later trigger actions.