+ Start a Discussion
imishraimishra 

Stop redirecting visualforce page if error is there.-urgent

Hi,
I have an object Closure(Custom Object) which is child of Opportunity.
I have a roll up summary field on Opportunity to count the no of closure and based on that a trigger which prevents a user to create more than one closure per opportunity and i have an error message on the trigger.
After saving the closure for the 1st time it should redirect to Opportunity record, but for second time on save, it should display the error message.
My save code is:
public PageReference saveAndReturn(){

             PageReference cancel = controller.cancel();
             controller.save();
             return cancel;

    }

Please help me to get this.

Thanks,
Ipsita
Vamsi KrishnaVamsi Krishna
Hi,

I m not sure how you implemented your SAVE method.. but if its throwing the error on saving second time , then you can do this

public PageReference saveAndReturn(){

             PageReference cancel = controller.cancel();
             try{
                 controller.save();
             }catch(Exception ex){
                 return null;
             }
             return cancel;

}

imishraimishra
Thanks for the reply.
I guess i was not clear with my requirement.
On save it is getting redirected.
But i want to stop the redirection if there is any error message on the page.
While creating the second record, it should disply the error message, but it is getting redirected there also. So for the second record i want to stop this redirection.
praveen murugesanpraveen murugesan
Hi imishra,

You can achieve that by this way.

public PageReference/Void/sobject getmethod(){
     pageReference page = new pageReferece('\apex\------');
     try
        {   
        //////////////save code
       
     
         }
       
     
        catch(Exception e){
         ApexPages.addMessages(e);
        }
        return ApexPages.hasMessages() ? null : page/URL/Record;
   }

If error occurs in try then catch block will add the error message in VF page.

In return we are returning the pageReference if page is not having any messages.

Mark this as solution if you got an answer,

Thanks,

Praveen Murugesan
imishraimishra
Can i create a command button in visualforce page.
On click of this, it should redirect me to the opportunity detail page.

Please reply. Its very urgent.
praveen murugesanpraveen murugesan
Hi Imi..

Sry for the delay. Yes you need to create

public PageReference saveAndReturn(){
        PageReference cancel;
            try
        { 
             cancel = controller.cancel();
             controller.save();
   
         }
     
   
        catch(Exception e){
         ApexPages.addMessages(e);
        }
        return ApexPages.hasMessages() ? null : cancel;   // Here we are returning to cancel if there is no error message.
   }

    }