自定义按钮或链接到带有自定义控制器的 Visualforce 页面 [英] Custom Button or Link to a Visualforce page with a custom controller

查看:52
本文介绍了自定义按钮或链接到带有自定义控制器的 Visualforce 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用自定义控制器的 Visualforce 页面,该控制器用于编辑机会下的多个记录.

I have a Visualforce page using a custom controller that is used to edit multiple records under an opportunity.

我想创建一个从商机到此 Visualforce 页面的自定义按钮或链接.

I'd like to create a custom button or link from Opportunities to this Visualforce page.

目前链接看起来像:

/apex/ExamplePage?oppId={!Opportunity.Id}

这在开发沙箱中工作正常,但当它作为托管包的一部分部署时,链接断开,因为页面引用没有命名空间前缀.

This works fine in the development sandbox, but when it is deployed as part of a managed package the link breaks as the page reference doesn't have the namespace prefix.

我发现了托管包重定向问题的帖子Force.com 讨论板暗示应该可以使用 $Page 来引用 URL 中的 Visualforce 页面.例如.

I found the post Managed Package Redirecting Problem on the Force.com Discussion Boards which implied it should be possible to use $Page to reference the Visualforce page in the URL. E.g.

{!URLFOR($Page.MyExamplePage,'',[objectId = campaign.id])}

但这样做只会给我带来语法错误:

But doing so only gives me the syntax error:

错误:字段 $Page.MyExamplePage 不存在.检查拼写.

Error: Field $Page.MyExamplePage does not exist. Check spelling.

帖子的另一部分建议使用 Apex 类并执行 Javascript 来解决它.但在我看来,这只是将命名空间问题转移到了 Javascript 中.

There is another part to the post that suggests using an Apex class and Execute Javascript to work around it. But it appears to me that this has just moved the namespace issue into the Javascript.

如何安全地引用 Visualforce 页面以在受管软件包内部和外部工作?

How can I safely reference the Visualforce page to work both inside and outside a managed package?

推荐答案

最好从 Apex PageReference 返回值执行此操作.这样的事情会起作用:

Best to do this from an Apex PageReference return value. Something like this will work:

public PageReference returnPage()
{
   return Page.MyExamplePage;
}

然后从 Visualforce 调用它:

Then call this from Visualforce:

<apex:commandButton value="Go To New Page" action="{!returnPage}"/>

Apex Page 呼叫将为您处理翻译.

The Apex Page call will handle the translation for you.

像这样创建一个简单的 Visualforce 页面:

Create a bare bones Visualforce page like this:

<apex:page standardController="Opportunity" extensions="TheController" action="{!returnPage}"/>

将上述 returnPage() 方法添加到新的 TheController(或其他)类.它甚至不需要构造函数.该类可以如下所示:

Add the above returnPage() method to a new TheController (or whatever) class. It doesn't even need a constructor. The class can look like this:

public TheController
{
   public PageReference returnPage()
   {
      return Page.MyExamplePage;
   }
}

然后从机会设置页面转到按钮和链接并创建一个新的自定义 Visualforce 按钮,选择您刚刚创建的新页面.

Then from the Opportunity settings page go to Buttons and Links and create a new custom Visualforce button selecting the new page you just created.

应该可以.

这篇关于自定义按钮或链接到带有自定义控制器的 Visualforce 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆