How to implement “Cancel” functionality in a VisualForce Page

好久不见. 提交于 2019-12-04 04:02:39

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.

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

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"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!