You need to sign in to do that
Don't have an account?

How to capture trigger error in controller extension
Hi All,
I am checking for the duplicate records using trigger. I have a VF page and controller extension to save the page.Could anyone please let me know how to capture trigger error in controller extension save method? When I enter duplicate record record is not saving, but it is not displaying error message defined in the trigger. I am using <apex:pageMessages to display error messages. Error message will be displayed only if I use standard "save" method , but not when I use controller extension "save" method. Could anyone please let me know what I need to change in the controller extension "save" method. I greatly appreciate your help.
public PageReference mysaveexit() { Sample__c individual = new Sample__c (); // insert individual; controller.save(); PageReference pageRef = new PageReference('/a07/o'); return pageRef; }
I've created a reduced version of your scenario and I'm seeing the same behaviour. It looks like the standard controller save method doesn't throw an error if the save fails, it simply returns a null page reference that would keep the browser on the edit page.
I think that if you change your method to return null if the standard controller method returns null, you'll get the behaviour you desire. Also, there's no need to add an error message, as adding an error in the trigger appears to populate a message that is displayed via the pagemessages component.
All Answers
What behaviour are you seeing when you use the controller extension? Do you not see an error message, or are you redirected to a standard error page.
I'd expect you to have to wrap the controller.save() in a try/catch block and then add the error into the page in order to get the errors to appear in the apex:pageMessages component.
Hi,
Thank you for your response. I could able to display error message on VF page.I am using following code to accomplish this. Now I need to set boolean Errorcondition == true in my trigger. Do I need to declare ErrorCondition boolean in trigger as a global variable? Could you please give me your suggestion on this. Please let me know how to declare this as a global variable in trigger so that it can accessed in my controllerextension class.Your help is greatly appreciated.
Usually the way you'd do this it to execute the addError method on the sobject(s) that your trigger is processig. This will cause the trigger to throw an exception on exit, which you can catch in your extension controller.
Hi,
Thank you for your response. I wrapped controller.save() method with try/catch block as follows:
In the trigger I am using addError method
In VF I am using
It is still not displaying error message on the VF page. I think exception is not cought in try/catch block in my controllerextension class. Could you please let me know what is wrong with the code? Your help is greatly appreciated.
What result are you seeing? If the error wasn't being trapped by the visualforce page I'd expect your browser to be redirected to a platform error page. Are you seeing that, or is your page simply refreshing?
Hi,
Thank you for your response.When I enter duplicate unique ID, it won't save the record.However, it will refresh and show page where I can create another record. This behaviour is according to my code in mysaveexit() method in the controller extension.Functionality is working fine.Somehow it is not displaying the error message.The addError() in the trigger is not cought in the try/catch block of the controller extension.I greatly appreciate your help.
Hmm. That's very strange. If the error wasn't being trapped, I wouldn't expect the page to be refreshed correctly.
Can you post the entire method - its a little tricky to figure out from snippets.
Hi,
Thank you for your response. Here is the trigger code
Here is my controller extension class
Please let me know where I am going wrong.Thank you for your help.
I've created a reduced version of your scenario and I'm seeing the same behaviour. It looks like the standard controller save method doesn't throw an error if the save fails, it simply returns a null page reference that would keep the browser on the edit page.
I think that if you change your method to return null if the standard controller method returns null, you'll get the behaviour you desire. Also, there's no need to add an error message, as adding an error in the trigger appears to populate a message that is displayed via the pagemessages component.
Hi,
It is a perfect solution!! It is displaying the message on VF page when I try to save duplicate records. Thank you very much for taking your time to work on this. I greatly appreciate your help.