How to implement "Cancel" functionality in a VisualForce Page
Asked Answered
M

4

7

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"/>
Mcgrew answered 19/1, 2012 at 5:25 Comment(0)
G
9

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

So you could just create an action method that returns a Pagereference with the appropriate URL and set to redirect (pr.setRedirect(true)).

Alternatively, you could 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"/>

Note that this doesn't necessarily take you to the list view, it'll return you to the last page you were viewing before going to the VF page.

Goodbye answered 19/1, 2012 at 5:31 Comment(3)
I always try to use the controller methods, since theoretically their protected from URL format changes.Antonelli
All the object IDs for records of that object will have the same 3 letters at the start, but as Jeremy says, it's best to use the standard actions where possible. You should be able to change your controller to an extension just by adding a standard controller param to the constructor, and modifying the <apex:page> tag so that it has standardController="MyObject__c" extensions="MyCustomController"Goodbye
This is not working when cancel ajax way by returning null. I save invalid data to year field and field give me error. when cancel it goes back to read only view but the data got changed to invalid data. However when refreshing page the invalid data is not there and previous data is displayedRajput
E
8

You should also add the immediate tag to your Cancel button, so that the form doesn't run any validation before performing the Cancel operation.

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

See http://blogs.developerforce.com/developer-relations/2008/12/using-the-immediate-attribute-on-commandlinks-and-commandbuttons.html

Endearment answered 21/5, 2012 at 13:12 Comment(0)
G
1

While applying cancel operation visualforce you should stop the form validation.Use below any one methods to stop the form validation based on your requirements.

Method 1:

Using html-5 in doctype in visualforce page means you should use html-formnovalidate and immediate in cancel button. For example

<apex:commandButton action="{!cancel}" value="Cancel" immediate="true" 
                    html-formnovalidate="formnovalidate" />

Method 2:

you should use immediate key word only need for stopping form validation. For Example

 <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
Gastric answered 10/4, 2015 at 7:8 Comment(0)
L
0

One of the other answers suggested calling the standard controller's cancel action so I want to expand on that since it led me in the direction to solve my similar problem.

If you want to cancel an edit as an ajax request without refreshing the whole page, declare the action as void and don't return the page reference, but still call the 'cancel' action on the standard controll. Make sure the command button specifies the rerender attribute.

// controller extension
public class TimeSheetExtension
{
  ApexPages.standardController m_sc = null;

  public TimeSheetExtension(ApexPages.standardController sc)
  {
    m_sc = sc;
  }

  public void doCancel()
  {
    m_sc.cancel();
  }
}

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

Lolalolande answered 13/4, 2021 at 1:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.