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/>