带有重定向的条件渲染

时间:2012-08-07 20:34:35

标签: salesforce apex-code visualforce

这是设置。我有一个带有自定义按钮的标准页面布局。

当用户点击按钮时,我想检查扩展类中的值。如果值为null或0-我想要显示页面消息,否则我想将它们重定向到另一个 VF 页面。

尝试执行此操作的方式是在页面布局上放置一个VF部分,并使用action:support方法有条件地呈现它,但我无法得到它工作..

HALP!

2 个答案:

答案 0 :(得分:0)

您无需在布局中添加VF页面 尝试使用Salesforce AJAX Toolkit作为自定义按钮:

1)首先创建一个WebService来计算你的值:

global with sharing class yourController{
    WebService static Integer calculateValue(Integer i) { 
        Integer result = i + 5;
        return result; 
    }
}

2)然后为对象创建自定义按钮:
显示类型 - 详细信息页面按钮
行为 - 执行JavaScript
内容来源 - onClick JavaScript

3)然后将此代码添加到按钮:

// Loading the ajax toolkit data
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}

// Making a call to the webservice method
var value = sforce.apex.execute("yourController","calculateValue", {i:5});

// If the value is 0 or blank            
if(value == 0 || value == ''){
    // Pop up a message
    alert('Your message text here');
else{
    // Otherwise redirecting user to another page
    window.location = "/apex/YourCustomPage";
}

4)现在保存按钮,不要忘记将其添加到布局页面:)

答案 1 :(得分:0)

如果从按钮调用的方法返回PageReference,则可以创建PageReference对象并使用.setRedirect( True)方法进行重定向。你现在可能正在返回Null,这表示“没有行动”。

您可以在创建PageReference时设置重定向的目标,或使用new ApexPages.StandardController()方法为给定对象类型选择一个控制器,或仅Page.vfPageName到引用另一个Visualforce页面。

当您的方法返回PageReference时,浏览器将重定向到新网址。

相关问题