Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I know that this is how to save a record

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

I want a button to NOT save the current record (ie. Cancel) and navigate to the list of saved record (ie. list of objects for that object type).

Something like this...

<apex:commandButton action="{!cancel}" value="Cancel"/>

1 Answer

0 votes
by (32.1k points)
edited by

The list view for an object is your base URL / the 3 letter prefix for your object / o, for example:

https://na1.salesforce.com/a0C/o

You can now go ahead and create an action which returns a Pagereference with an appropriate URL and set it to redirect (pr.setRedirect(true)).

Want to learn Salesforce from the scratch? Here's is the right video for you on Salesforce provided by Intellipaat:

Otherwise, you can use your controller as an extension to a standard controller, and just call cancel on the standard controller:

// controller extension

public class TimeSheetExtension

{

  ApexPages.standardController m_sc = null;

 public TimeSheetExtension(ApexPages.standardController sc)

  {

    m_sc = sc;

  }

  public PageReference doCancel()

  {

    return m_sc.cancel();

  }

}

// page

<apex:commandButton action="{!doCancel}" value="Cancel"/>

Another thing is that this won't take you to the list view. Rather, it will return you to the last page which you were viewing before going to the VF page. 

If you are preparing for the Salesforce admin certification exam, you can take up this Salesforce training by Intellipaat!

Browse Categories

...