+ Start a Discussion
emandra_kfemandra_kf 

Try/Catch not "catching" a Validation Error

I'm using a controller extension with a custom object to provide a SaveandReturn method. The code is basically a copy/paste from other similar controllers posted on this board. My problem is that the Catch portion of the Try/Catch never gets invoked - even when there is a clear validation error. My VF page includes the <apex:PageMessages /> tag. The controller looks like this:

 

public with Sharing class SaveAndReturnController
{
    private ApexPages.StandardController controller;

    public SaveAndReturnController(ApexPages.StandardController controller)
    {
        this.controller = controller;
    }

    public PageReference saveAndReturn()
    {
        try {  
        
           PageReference cancel = controller.cancel();
           controller.save();
           System.Debug('Saving');
           return cancel;  
        }
           
           // Don't redirect if something goes wrong. May be a validation or trigger issue on save.
         catch(exception e) {
            ApexPages.addMessages(e);
            System.Debug('Exception');
            return null;  }
    }
}

 

When the Save button is clicked, the record is NOT saved and the page is redirected to the parent page. No error message is ever displayed. Does anyone have a suggestion about how to catch the Validation errors when they occur?

 

Best Answer chosen by Admin (Salesforce Developers) 
emandra_kfemandra_kf

Ram - thank you very much! Your second suggestion got me to the right solution. Enclosed is the method that worked:

 

    public PageReference saveAndReturn()
    {
        try {  
           PageReference cancel = controller.cancel();
           PageReference pr = controller.save();
           System.Debug('Saving');
            if (ApexPages.hasMessages()) {
                return pr;
            } else {
                return cancel;         
            }
        }
           // Don't redirect if something goes wrong. May be a validation or trigger issue on save.
         catch(exception e) {
            ApexPages.addMessages(e);
            System.Debug('Exception');
            return null;  }
    }

 

The only thing I had to do was put the PageReference cancel =  controller.cancel(); statement before the controller.save() statement; otherwise, the pagereference went to the new record's View page rather than the parent page. Also, I left the whole Try/Catch apparatus in place just in case it catches some other error someday; not sure if it's really necessary, but it's not hurting anything so I left it there. Thank you again for your help!

 

All Answers

Bhawani SharmaBhawani Sharma
How you are calling this from page ?
ForcepowerForcepower

emandra,

 

try this:

    public PageReference saveAndReturn()
    {
        try {  
        
           PageReference nextPage = controller.save();
           System.Debug('Saving');
           return nextPage;  
        }

 

instead of

 

    public PageReference saveAndReturn()
    {
        try {  
        
           PageReference cancel = controller.cancel();
           controller.save();
           System.Debug('Saving');
           return cancel;  
        }

 

Also, add <apex:messages /> to your VF page so that any validation errors are shown on that page.

 

Best,

Ram

emandra_kfemandra_kf

I'm calling it with a command button in my VF page:

 

<apex:commandButton value="Save" action="{!saveAndReturn}"/>

emandra_kfemandra_kf

Ram, thank you for the suggestion - it partially worked. The validation error message is now displaying as desired, but the intent of the controller is to "return" to the parent page in lieu of the standard controller's default action, which is to display the new record in View mode.

 

With this approach, controller.save() method simply reverts to the default action and presents the new record in View mode - it does not redirect to the parent page like controller.cancel().

 

Any thoughts on how to get back to the parent page?

ForcepowerForcepower

emandra,

 

try this:

 

        try {  
        
           PageReference pr = controller.save();
           System.Debug('Saving');
           if (ApexPages.hasMessages())
               return pr;
           else {
                   PageReference cancel = controller.cancel();         
                   return cancel;      
           }  
        }

ForcepowerForcepower

Hopefully that works for you. If controller.cancel() does not take you back to the desired page, you'll need to do something with a retURL parameter to take you back to where you need to go.

Ram

emandra_kfemandra_kf

Ram - thank you very much! Your second suggestion got me to the right solution. Enclosed is the method that worked:

 

    public PageReference saveAndReturn()
    {
        try {  
           PageReference cancel = controller.cancel();
           PageReference pr = controller.save();
           System.Debug('Saving');
            if (ApexPages.hasMessages()) {
                return pr;
            } else {
                return cancel;         
            }
        }
           // Don't redirect if something goes wrong. May be a validation or trigger issue on save.
         catch(exception e) {
            ApexPages.addMessages(e);
            System.Debug('Exception');
            return null;  }
    }

 

The only thing I had to do was put the PageReference cancel =  controller.cancel(); statement before the controller.save() statement; otherwise, the pagereference went to the new record's View page rather than the parent page. Also, I left the whole Try/Catch apparatus in place just in case it catches some other error someday; not sure if it's really necessary, but it's not hurting anything so I left it there. Thank you again for your help!

 

This was selected as the best answer
ForcepowerForcepower
ahh - interesting. Looks like controller.cancel() changes after the save() to point to the View but would point to the parent page if done prior. Excellent. Glad to hear it, emandra.
Best,
Ram