Salesforce中隐藏批准历史相关列表(Related List)中的“提交审批”按钮 - 方案与实践

437 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

场景描述

在配置或开发批准过程时,业务中经常需要在点击提交审批按钮时对当前单子状态作校验,并提示客户在什么条件下才能提交审批。那通常的做法是自定义提交审批按钮,执行JS方法,调用SOAP Web Service的校验逻辑。

平台特性

在Salesforce中,标准页面上的审批历史相关列表上的提交审批按钮不能通过配置的方式移除。

问题引入

基于前面的介绍,我们会面临一个尴尬的问题:自定义按钮展示在Detail页面上,标准按钮展示在Related List页面上,一旦用户点击了Related List上的按钮,那这个自定义按钮放在上面就没能达到预设的目标了。

解决方案

通常我们会通过将审批初始提交人设为超级管理员或加一个checkbox控制准入条件,这样业务人员不管在什么条件下点击了Related List上的按钮都会进入到无效的访问权限页面。

使用checkbox方法思路

  1. 新建checkbox字段(是否为标准审批按钮),默认unchecked;- 若默认为checked,那历史数据不会勾上。
  2. 配置准入条件,该字段为true,才允许进入;
  3. 代码中设置req.setSkipEntryCriteria(true),用来跳过进入条件即可。 180511更新:后续找到更合适的方法,可以直接用公式将准入条件变为false,如下:

其他3种方案

  1. 自定义一个VFP(仅仅包含批准历史相关列表),然后在VFP里面使用JS Hack去隐藏按钮;

效果预览:

实施细节:
1.1 开发VF logic code:

<apex:page standardController="ApprovalTest__c">
    <script src="https://code.jquery.com/jquery-2.x-git.min.js"></script>
    <script>
        $j = jQuery.noConflict();
        console.log(window.location.href);
        console.log(window.location.href.indexOf(".com/a0C"));
        console.log(window.location.href.indexOf(".com/a0C") != -1);
        if(window.location.href.indexOf(".com/a0C") == -1) {
            $j(document).ready(function() {    
                $j("input[name='piSubmit']").hide();
            });
        }
    </script>
    <apex:relatedList list="ProcessSteps" id="customApprovalList"></apex:relatedList>
</apex:page>

1.2 将页面VF加入页面布局:

之后,you make it.

  1. 在Home Page的sidebar中注入Js,通过JS hack的方式隐藏Related List上的提交审批按钮;

I have already gone through solution here [Display "Submit for Approval" button in the Approval History related list based on condition](salesforce.stackexchange.com/questions/2… "Display "Submit for Approval" button in the Approval History related list based on condition") but since new release summer 15 home page component no more supports JS , so that does not worked for me.

看到上面一句话不禁有些遗憾,summer 15之后不能在html类型的home component中加JS。

实施细节:
2.1 自定义injection Custom Links;- 注意静态资源的引用

{!REQUIRESCRIPT("/resource/1402932484000/JQuery320")} 
$j = jQuery.noConflict(); 
if(window.location.href.indexOf(".com/a0C") != -1) { 
$j(document).ready(function() { 
$j("input[name='piSubmit']").hide(); 
$j(".relatedProcessHistory .pbHeader h3").css('padding', '8px 0'); 
}); 
}

2.2 在自定义Link类型主页组件里添加injection custom link;

2.3 在主页布局中启用injection-box自定义组件。

效果预览:

最终你会发现,这种方式你需要点击sidebar自定义链接后才能实现隐藏的效果。

3. 通过在VF类型home page component中加入js代码,不可行。原因:不能在iframe操作dom。

总结

首推配置的方式,其次是方法1。

参考资料