function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JoeyDJoeyD 

Save Button to Redirect user

I'm using this bit of code to allow a save button on a page to save a list, but I'd like the button to redirect the user to a different page after they click Save, much like the Cancel button does now.

 

 

public PageReference cancel() {
          PageReference pr = new PageReference('/a0G/o');
          pr.setRedirect(true);
          return pr;
          }
    public void save() {
    
        try{
            update pfc;
        }
        catch(DmlException ex){
            ApexPages.addMessages(ex);
            
        }
        }
           

 

 

The save method works, in that what is edited in the list saves, I would just like to add the same PageReference bit from the cancel method to the save method, and I don't know how to do that. :smileyindifferent:

 

Ideally both cancel and save buttons would direct the user back to the List View from where the visualforce page that uses this class is accessed, but simply redirecting back to the object URL is fine.

 

Any help would be greatly appreciated!

 

Best Answer chosen by Admin (Salesforce Developers) 
sunil316sunil316

Same way as u did in cancel button. Just put PageReference code after your save method code,

 

public void save() 
{
try{
update pfc;
}
catch(DmlException ex){
ApexPages.addMessages(ex);

}
      PageReference pr = new PageReference('/a0G/o');
pr.setRedirect(true);
return pr;
}

All Answers

sunil316sunil316

Same way as u did in cancel button. Just put PageReference code after your save method code,

 

public void save() 
{
try{
update pfc;
}
catch(DmlException ex){
ApexPages.addMessages(ex);

}
      PageReference pr = new PageReference('/a0G/o');
pr.setRedirect(true);
return pr;
}
This was selected as the best answer
JoeyDJoeyD

Thank you so much, it works great!

 

I did have to replace public void save()

with public PageReference save(), as the former was returning an error stating something about a void method returning something or other. :)

 

Thanks again!

BManojKumarBManojKumar
Hi JoeyD, Can you please guide me where to create this method..?
BManojKumarBManojKumar

Hi JoeyD,

 

I got it how to create the method.. Can you tell me what the "update pfc;" will do ?

JoeyDJoeyD

pfc was my variable for a list of records.  The save method updates the changes made to the input fields found between the same form tags on the visualforce page.

BManojKumarBManojKumar

Thank You JoeyD.. Good Day..