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
Kent LichtyKent Lichty 

How to cancel (return) back to a custom list view?

I have really appreciated all of the assistance that I have received through this forum, so I thought I would go to the well once again and ask my "newbie" question:  I have a custom vf and apex application (one each for "view" and "edit").  I have created a new list view that contains my preferred fields, as opposed to the standard list view.   I have overridden the "View" and "Edit" buttons from my object's list view to call my custom pages.  On my custom pages I have a standard "Cancel" button which, when pressed, I want to return the user to my NEW list view and NOT to the standard object's list view.  When I click on the "Edit" button (at the far right, in the inverted triangle) to access my custom page, and then click my "Cancel" button it works perfectly; returning me to my custom new list view.  I have NOT overriden the Cancel method in Apex at all.  However, when I access my "View" page from the blue hyperlinked item the "Cancel" button returns me back to the STANDARD list view as opposed to my NEW list view.  Also, I had to include a "Cancel" method in my Apex code with this code:

return new ApexPages.Action('{!List}').invoke(); 

 or it would not even return to ANY list, but stayed on the same page. 

My question is:  why do I need to override the "Cancel" method at all, as I thought it's standard behavior would be to return to the MOST RECENT page, which should be my new list view?   Is there some way that I can safely and securly capture the URL of the previous list view so I can easily return to it? 

I know this got pretty long, but I would appreciate your insights on this.  Thanks very much.

 

ShirishaShirisha (Salesforce Developers) 
Hi Kent,

Greetings!

Can you please try the below sample code without hardcoding the URL for the last page:

Visualforce Page Button:
1<apex:commandButton action="{!back}" value="Back"/>
Apex Controller:
public with sharing class ExampleController
{
    private ApexPages.StandardController sctrl;
    public SaveAndReturnController(ApexPages.StandardController stdController)
    {
        this.sctrl = stdController;
    }
  
    public PageReference back()
    {
        PageReference cancel = sctrl.cancel();
        return cancel;
    }
}

Please check the code here (https://www.biswajeetsamal.com/blog/redirecting-back-to-previous-page-using-visualforce-page-standardcontroller/) for your reference.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri