Salesforce 如何实施";取消“;VisualForce页面中的功能

Salesforce 如何实施";取消“;VisualForce页面中的功能,salesforce,apex-code,visualforce,force.com,Salesforce,Apex Code,Visualforce,Force.com,我知道这就是保存记录的方法 <apex:commandButton action="{!save}" value="Save"/> 我想要一个按钮,不保存当前记录(即取消),并导航到已保存记录列表(即该对象类型的对象列表) 像这样的 <apex:commandButton action="{!cancel}" value="Cancel"/> 对象的列表视图是您的基本URL/对象的3个字母前缀/o,例如: https://na1.salesforce.com/a0

我知道这就是保存记录的方法

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

我想要一个按钮,不保存当前记录(即取消),并导航到已保存记录列表(即该对象类型的对象列表)

像这样的

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

对象的列表视图是您的基本URL/对象的3个字母前缀/o,例如:

https://na1.salesforce.com/a0C/o
因此,您可以创建一个操作方法,该方法返回带有适当URL的
Pagereference
,并设置为重定向(
pr.setRedirect(true)

或者,您可以将控制器用作标准控制器的扩展,只需:

//控制器扩展
公共类时间表
{
ApexPages.standardController m_sc=null;
公共时间表(ApexPages.standardController sc)
{
m_sc=sc;
}
公共页面引用doCancel()
{
返回m_sc.cancel();
}
}
//页面

请注意,这并不一定会将您带到列表视图,它会将您返回到VF页面之前查看的最后一页。

您还应该将立即标记添加到取消按钮,以便表单在执行取消操作之前不会运行任何验证

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

应用取消操作visualforce时,应停止表单验证。根据您的要求,使用以下任意一种方法停止表单验证

方法1:

使用 visualforce页面doctype中的html-5 意味着您应该在“取消”按钮中使用html formnovalidate
immediate
。比如说

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

另一个答案建议调用标准控制器的取消操作,因此我想进一步说明,因为它引导我解决了类似的问题

如果要取消作为ajax请求的编辑而不刷新整个页面,请将该操作声明为void,不返回页面引用,但仍在标准控件上调用“cancel”操作。确保命令按钮指定了重新渲染器属性

// 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"/>

//控制器扩展
公共类时间表
{
ApexPages.standardController m_sc=null;
公共时间表(ApexPages.standardController sc)
{
m_sc=sc;
}
公开作废文件
{
m_sc.cancel();
}
}
//页面

我总是尝试使用控制器方法,因为理论上它们不受URL格式更改的影响。该对象记录的所有对象ID在开始时都有相同的3个字母,但正如Jeremy所说,最好尽可能使用标准操作。您应该能够将控制器更改为扩展,只需向构造函数添加一个标准控制器参数,并修改
标记,使其具有
standardController=“MyObject\uu c”extensions=“MyCustomController”
当通过返回null取消ajax方式时,这不起作用。我将无效数据保存到“年份”字段和“字段给我错误”。取消时,它返回只读视图,但数据已更改为无效数据。但是,刷新页面时,无效数据不存在,并且显示以前的数据
 <apex:commandButton action="{!cancel}" value="Cancel" immediate="true"/>
// 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"/>