zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Salesforce 使用HYPERLINK FORMULA FIELD实现自定义跳转

2023-03-15 22:01:00 时间

a.数式项目做成

数式内容:

HYPERLINK("https://www.baidu.com/", "baidu1")

b.DB中检索一下,看看存储的数据是什么样子

根据检索结果【target="_blank"】,会跳转到一个新的Tab。

检索结果:

<a href="https://www.baidu.com/" target="_blank">baidu1</a>

c.详细Page中,把自定义项目拖进来,进行测试

结果跟预想的一样,会打开一个新的Tab。

※自画面跳转的情况下,可以指定参数

HYPERLINK("https://www.baidu.com/", "baidu1", '_self')

HYPERLINK("https://www.baidu.com/", "baidu3", '_top')

2.实际应用例

上边讲的方式帮助我们实现了页面跳转,在实际项目应用中,跳转逻辑相对复杂,这种情况下,我们可以考虑把逻辑处理写在后台ApexClass中。

a.首先为了测试,需要创建两个跳转用的自定义数式项目,然后准备两个参数,用于后台判断用。

在Opportunity详细Page上配置两个项目,其中项目1用于跳转到【https://bing.com/】页面,

项目2用于跳转到【https://cn.bing.com/translator?mkt=zh-CN】。

项目1数式内容:

HYPERLINK("/apex/MyLinkForVF?recordId="+Id+"&linkId=linkId001", "GoTo Bing")

项目2数式内容:

HYPERLINK("/apex/MyLinkForVF?recordId="+Id+"&linkId=linkId002", "GoTo Bing translator")

b.创建VFPage与对应的ApexClass,用于逻辑判断

MyLinkForVF.page

<apex:page controller="MyLinkForVFController" action="{!navigationToLink}">
<!-- Begin Default Content REMOVE THIS -->
<h1>Congratulations</h1>
This is your new Page
<!-- End Default Content REMOVE THIS -->
</apex:page>

MyLinkForVFController

public with sharing class MyLinkForVFController {
    public Id recordId {get; set; }
    public String linkId {get; set; }
    public String resultURL { get; set; }
    public Boolean isMobile { get; set; }

    public MyLinkForVFController() {
        String recordId = ApexPages.currentPage().getParameters().get('recordId');
        String linkId = ApexPages.currentPage().getParameters().get('linkId');

        String header= ApexPages.currentPage().getHeaders().get('User-Agent');
        String apiName;
        String recordIdPrefix = recordId.substring(0, 3);
        Map<String,Schema.SObjectType> globalDescribeMap = Schema.getGlobalDescribe();
        for(String objectN : globalDescribeMap.keySet()) {
            Schema.DescribeSObjectResult result = globalDescribeMap.get(objectN).getDescribe();
            String prefix = result.getKeyPrefix();
            if(String.isNotBlank(prefix) && prefix.equalsIgnorecase(recordIdPrefix)) {
                apiName = result.getName();
            }
        }
        if (header.toLowerCase().contains('iphone')) {
            isMobile = true;
        } else {
            isMobile = false;
            if ('Opportunity'.equalsIgnoreCase(apiName)) {
                if ('linkId001'.equalsIgnoreCase(linkId)) {
                    resultURL = 'https://bing.com/';

                } else if ('linkId002'.equalsIgnoreCase(linkId)) {
                    resultURL = 'https://cn.bing.com/translator?mkt=zh-CN';

                }
            } else if ('Account'.equalsIgnoreCase(apiName)) {
                resultURL = 'https://www.baidu.com/';
            }
        }
    }
    public PageReference navigationToLink() {
        if(isMobile) {
            Pagereference pageRef;
            //pageRef = Page.xxxx;
            pageRef.getParameters().put('errorMessage', 'Mobile Access Error');
            pageRef.setRedirect(true);
            return pageRef;
        } else {
            PageReference pageRef = new PageReference(resultURL);
            pageRef.setRedirect(true);
            return pageRef;
        }

    }
}

效果展示: