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

Custom Controller "Save and back" action


I have a list button which passes the record id to a visualforce page.
This visualforce page calls a simple custom controller that displays and saves records.

This is working absolutely fine.

However a very basic question ,how do i have a "Cancel" button action in a custom controller,wherein when i click on Save ,After the save action this should go back the previous page?

The save part of my custom controller:

 public PageReference save() {
            update pserial;
                 return null;
//instead of returning null,i need to get back to the previous page,how do i set the page reference here?


vf page:

<apex:page controller="iresv">
<apex:form >
<apex:pageMessages ></apex:pageMessages>

<apex:pageBlock >
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!save}" value="Save" />
<apex:pageBlockTable value="{!Serial}" var="s">

<apex:column value="{!s.Product_disp__c}"/>
<apex:column headerValue="Reserve">
<apex:inputCheckbox value="{!s.Reserved__c}"/>


Please help!

Best Answer chosen by Admin (Salesforce Developers) 
Imran MohammedImran Mohammed

Make this change. It should work.

window.location.href= '/apex/iresv?id='+records[0] +'&pid={!Order__c.Id }';

All Answers




If you know the previous page name then you can do something like



PageReference pre = new PageReference('/apex/pagename');        
return pre; 


Let me know if you need additional help.





The button is  a list button on an object.So i need to go back to that detail page....


Here the object is Order__c which has child Order_line__c.


For the selected order line id(using a list button with checkbox) the vf page is opned...


Is there anyway i can store the page reference somehow before and then redirect to that page reference?



Imran MohammedImran Mohammed

In that case what you can do is when the child record is clicked, add query string parameter to the VF page URL you navigate to.

Add the id of Order__c record as a query string parameter to the URL.

In Controller code, get that the parammeter and save it to a variable which you can use while navigating back to detail page.

String pid = ApexPages.currentPage().getParameters.get('parentid');//assuming parentid is query string param which holds id of Order__c record.

for ex:

Pagereference p = new PageReference('/'+pid);


return p;


Let me know if you face any issues.


Hi ,

I have modified the controller code as suggested  and the page is redirecting to the previoues order page perfectly.

However Iam adding the "&parentid=a0CR0000002OTJk" part manually in the browser.

How do i pass it automatically from the list button?

My javascript list button code:


var records = {!GETRECORDIDS($ObjectType.Order_Line__c)};

if (records.length == 0)
alert("Please select at least one order line");
else if(records.length >1)
alert("Inventory reserve can be created for one product at a time ");


window.location.href= '/apex/iresv?id='+records[0] ;

//how do i get the parent id part here so that when the redirection to vf happens the &parentid part is automatically present appended as a query string parameter in the browser





public class iresv {
public  List<Product_serial__c> pserial=new List<Product_serial__c>();
String pid = ApexPages.currentPage().getParameters().get('parentid');
public iresv(){
pserial=[select id, name,serial_number__c,product_disp__c,scheduled_date__c,reserved__c from product_serial__c where product__c in (select product__c from order_line__c where id =
                       :ApexPages.currentPage().getParameters().get('id')) and resvdone__c=FALSE and order__c in (select order__c from order_line__c where id =
                       :ApexPages.currentPage().getParameters().get('id')) ];


  public List<Product_serial__c> getSerial() {
            return pserial;
      public PageReference save() {
            update pserial;
            Pagereference p = new PageReference('/'+pid);

            return p;               



Imran MohammedImran Mohammed

When you go to the List button, you will find a drop down which shows the objects.In that you will find the parent object.

Select that and select record id from Insert Field drop down.

Use that in the URL

/apex/iresv?id='+records[0]+'&pid='+{!Merge Field of Parent}

Let me know if it works. 

Imran MohammedImran Mohammed

Did you get that or you are facing any issues?


Hi Imran,


I added the following:

window.location.href= '/apex/iresv?id='+records[0] +'&pid='+{!Order__c.Id };


But now when i click the button with a record selected,

I get the following error:


"A problem with the Onclick Javascript for this button or link was encountered:a0CR0000002OTNA is undefined"



this is the id of the parent  of the selected child record,


Some syntax issue?

Imran MohammedImran Mohammed

Make this change. It should work.

window.location.href= '/apex/iresv?id='+records[0] +'&pid={!Order__c.Id }';

This was selected as the best answer

Worked Perfectly!


Really appreciate your help on this Imran!

Thanks a million!