1

I have a page using an extension to the standard controller, when the records are saved using the standard save method, an after insert trigger is fired and does some validation, I have created a handler for the trigger with a custom exception, if the exception is thrown, I want to present a message to the user and roll back the operation.

The problem is that I see the exception thrown in the debug log but the records are still committed, how can I present the error in the page and stop the operation?

Here is a snippet of what I am doing in the trigger handler:

try{
            for(mycustomobject__c mix:parentsandchildren)
            {
                 List<childobject__c> childobjects = parent.getSObjects('childobjects__r');
                 Integer childobjectcounter=0;
                 for(childobject__c childobject:childobjects)
                {
                    childobjectcounter++;                   
                    if(childobjectcounter>maxchildobjectlimit)                  {   
                        throw new MaxchildobjectLimitException('Bla bla bla');              
                        break;
                    }
                }
            }
        }
        catch(MaxchildobjectLimitException maxLimit){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error, 'Limit Reached!'));
        }

In the VF page I have this:

<apex:pageMessages/>
  • I think to automatically roll back you will need to use Add Error in trigger catch block and also this will automatically display.Are you using Add Error in catch block for trigger ? – Mohith Shrivastava Apr 23 '14 at 15:21
  • I was trying to do it with the adderror method but I was getting an error because the object from where I was adding the error was not in the trigger context. I have resolved this in a different way (without this code) but I am still wondering if there is a way to stop the operation after a custom exception is thrown. – PepeFloyd Apr 23 '14 at 15:56
2

I use this pattern a lot for controllers and controller Extensions that do DML. It provides for multi-step DML operations with decent diagnostics as to where the error occurred in such multi-step sequence.

If the exception occurs in the trigger on Foo or Bar, the trigger can throw the exception by either explicitly using throw new Exception or it can use addError (more likely) or there could be a Validation Rule error (which will implicitly do an addError). In all cases, the controller will catch an exception and do the rollback

// ----------------------------------------------------------------------------------------
// ACTION METHOD    - my action method
// ----------------------------------------------------------------------------------------
public PageReference    myActionMethod() {
    PageReference           resPg;
    Boolean                 isSuccess       = true;
    Savepoint               sp;
    String                  step;   
    try {                           
        sp      = Database.setSavePoint();
        step    = '01 - Create Foo';
        Foo__c foo = someMethodToBuildFoo();
        insert foo;
        step    = '02 - Insert Bar';
        Bar__c bar = someMethodToBuildBar();
        insert bar;
    }
    catch (Exception e) {
        if (sp != null) Database.rollback(sp);
        isSuccess   = false;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,
            '[ERR-00] Insertion of Foo+Bar failed at step: ' + step + ', reason: ',e));

        }

    if (isSuccess) {
        resPg       = new PageReference('/'+someIdField);
        resPg.setRedirect(true);
    }   
    return resPg;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.