在Salesforce标准的列表视图中,如果想要做一些自定义的操作,如选择某条记录后,将记录的详情发送到指定邮箱,我们把按钮标放在跟标准“新建”按钮同一位置,点击后执行特定的逻辑
方法一:用VisualForce页面实现该功能
1.创建VisualForce页面
<apex:page standardController="Account"
extensions="Controller_Account"
recordSetVar="accounts"
lightningStylesheets="true">
</apex:page>
2.页面控制类
public without sharing class Controller_Account {
public Controller_Account(ApexPages.StandardSetController controller) {
System.debug(controller.getSelected());
}
}
3.新建、配置列表按钮
4.调试
拿到的记录是一个列表,之前的实验币种字段也能拿到,上图可以看到的是Id和记录类型字段,具体哪些字段没研究,Id字段能拿到再去SOQL查询就不是问题了
5.踩坑
(1)后续的逻辑正常来说可以直接在构造方法继续编写,最后处理完后,弹窗提示,这是网上提供的方法,直接在Apex类控制VisualForce页面弹窗提示
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, '操作成功'));
(2)这个方法,我试了一直不成功。所以,我被迫在VF页面写了一个方法,调用控制了方法,在VF写弹窗方法,根据返回体提示信息
<script>
function showToast(title, message, type) {
sforce.one.showToast({
"title": title,
"message": message,
"type": type,
"duration": 3500
});
}
</script>
(3)提示完成后,最后的步骤还需要返回到列表视图页面。跳转的方法有2种,一是JS跳转,二是VF提供的方法跳转
<script>
window.parent.location.href = '/lightning/o/Account/list'; // JS跳转
sforce.one.navigateToList('00B5i000003241uEAA', '新建本周', 'Account'); // 参数:列表视图Id,列表标签(可为null),跳转的对象
</script>
(4)列表视图的Id在构造方法中获取,在Apex拿到Id后,传给VF页面,VF页面必须设置一个标签接收值(很明显,用JS跳转比较省事,除非SF的Url规则发生改变)
VisualForce页面
<apex:page standardController="Account"
extensions="Controller_Account"
recordSetVar="accounts"
lightningStylesheets="true">
<input type="hidden" id="id-1" value="{!filterId}"></input>
<script>
let id = document.getElementById('id-1').value;
sforce.one.navigateToList(id, '', 'Account');
</script>
</apex:page>
Apex类
public without sharing class Controller_Account {
public String filterId {get;set;}
public Controller_Account(ApexPages.StandardSetController controller) {
System.debug(controller.getSelected());
System.debug(controller.getListViewOptions()); // 获取所有列表视图参数
System.debug(controller.getFilterId()); // 获取当前列表视图Id
this.filterId = controller.getFilterId(); // 保存视图Id
}
}
运行日志
参考文档
[1]. Navigation and Messaging with the sforce.one Object
[2]. How to get selected records from list view in Salesforce