Apex salesforce 页面引用 URL 链接合并字段



>假设创建了一个链接,并指向具有以下模式的URL:/00O70000001SOsa?pv0={!Account.Id}&pv1={!案例已关闭} 如果我必须使用页面引用或任何其他类似的 API 调用相同的内容,如何在 Apex 控制器中编写它。

// You will have to apply your own logic on how you pick data for the link
Account acc = [SELECT Id FROM Account LIMIT 1];
Case c = [SELECT IsClosed FROM Case LIMIT 1];
Pagereference pr = new PageReference('/00O70000001SOsa');
pr.getParameters().putAll(new Map<String, String>{
'pv0' => acc.Id,
'pv1' => c.isClosed ? 'true' : 'false'
});
System.debug(pr.getUrl());
// outputs something like /00O70000001SOsa?pv0=0017000001TSmKmAAL&pv1=false

传递给 URL 的参数必须是字符串(或容易转换为字符串的内容(。因此,您的工作是转换布尔值,以其他页面"喜欢"的格式显示日期(报表需要与用户使用的格式相同的日期,因此根据他/她的区域设置,请查看日期和时间时间类中的format方法(。

但仅此而已。您不必担心"?"符号或任何特殊字符的花哨转义,它会为您完成:

Pagereference pr = new PageReference('/home/home.jsp');
pr.getParameters().putAll(new Map<String, String>{
'spaces' => 'spa ce',
'html-entities' => '& < >',
'percents' => '1 / 4 = 25%'
});
System.debug(pr.getUrl());
// /home/home.jsp?html-entities=%26+%3C+%3E&percents=1+%2F+4+%3D+25%25&spaces=spa+ce

最新更新