前言
博主也是Salesforce Apex 工作不久,有用到的知识和平时查阅到的知识和大家分享一下。
总结
1、使用SOQL语句查询时,字符串类型的只能使用‘单引号’,否则报错:Unknown error parsing query;
eg:SELECT Id, Subject, CaseNumber, Status, Priority
FROM Case
WHERE CaseNumber = '00001036' //注意此处autoNumber类型数字为String类型
使用SOQL其他注意事项:
- 尽量使用Order By,保证查询结果的稳定性;
- 使用LIMIT,防止数据量超过50000而报错;
- 使用Offset偏移时,一定放在该查询语句的最后面,其限制及应用场景见:传送门
- 标准字段的API Name即为该标准字段的Field Name;
eg:Case标准对象的Subject API Name即为 Subject
- 计算两个日期之间相隔的天数:
eg:TODAY() - DATEVALUE( CreatedDate )
Implementd__Date__c - DATEVALUE( CreatedDate )
注意:只有当日期为标准字段时,才使用DATEVALUE()来转化
- 在terminal中使用curl方式查看json数据(通常使用workbench>utilities>REST Explorer),通常会用到sessionId,获取方法如下:
String sessionID = UserInfo.getSessionId();
System.debug(sessionID);
- 完整版REST services demo
@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {
@HttpGet
global static Case getCaseById() {
RestRequest req = RestContext.request;
String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Case result = [SELECT CaseNumber, Subject, Status, Origin, Priority
FROM Case
WHERE Id = :caseId];
return result;
}
/*
HttpGet步骤:
1、创建RestRequest类型的req对象(RestContext.request的返回值类型就是RestRequest)
2、通过req对象的requestURI属性利用字符串检索技术拿到caseId
3、创建Case对象result,并将通过caseId查到的记录赋值给该对象,注意“WHERE Id = :caseId”
4、返回Case对象
*/
@HttpPost
global static ID createCase(String subject, String status,
String origin, String priority) {
Case thisCase = new Case(
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
insert thisCase;
return thisCase.Id;
}
/*
HttpPost步骤:
1、声明并创建一个Case类型对象thisCase,并为该对象的标准字段赋值
2、将自定义对象插入到Case表中形成一条记录
3、返回一个新纪录的类型为ID的变量Id用于查找新纪录
*/
@HttpDelete
global static void deleteCase() {
RestRequest req = RestContext.request;
String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
delete thisCase;
}
/*
思路:
要删除某一条记录首先要找到该记录,而方法可以是利用soql语言查找到某一记录的主码,这里是Id(使用rest服务请求获取到uri后从uri中取得的id)
HttpDelete步骤:
1、创建ResrRequest对象req
2、声明caseId,并将rest请求到的uri截取/后的值赋给该变量
3、利用soql语句查到Id = :caseId的那条记录
4、删除该记录
*/
@HttpPut
global static ID upsertCase(String id, String subject, String status, String origin, String priority) {
Case thisCase = new Case(
Id = id,
Subject = subject,
Status = status,
Origin = origin,
Priority = priority
);
upsert thisCase;
return thisCase.Id;
}
/*
HttpPut步骤:
1、声明并创建一个Case类型对象thisCase,并为该对象定义标准字段赋值
2、将自定义对象插入到Case表中形成一条记录或者更新Id为id的记录
3、返回一个新纪录的类型为ID的变量Id用于查找新纪录
*/
@HttpPatch
global static ID updateCaseFields() {
RestRequest req = RestContext.request;
String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(req.requestBody.toString());
for(String fieldName : params.keySet()) {
thisCase.put(fieldName, params.get(fieldName));
}
update thisCase;
return thisCase.Id;
}
/*
HttpPatch步骤:
1、创建RestRequest类型的req对象(RestContext.request的返回值类型就是RestRequest)
2、通过req对象的requestURI属性利用字符串检索技术拿到caseId
3、创建Case对象,并把按Id查到的Case表记录赋值给该对象
4、将请求获得的requestBody转化成字符串后,反序列化为对象强制转化为Map<String, Object>后赋值给Map变量params
5、遍历对象的key,并在通过id找到的Case对象thisCase中写入key-value
6、更新记录
7、返回记录的id
*/
}
/*
共性:
1、每个对象系统自带一个Id属性,它是系统自动分配的;
2、每一种Http方法均为global static
3、@HttpPut与@HttpPost的区别(upsert,insert)
*/
区别:put vs patch
the same:You can update records with the put or patch methods.
the difference: put can either create new resouce or update the existing resource; patch can update the existing resouce exclusively.
-
Apex中在使用类继承时需要使用到的关键字:extends,super,virtual,override.跟Java继承不同的是,超类必须使用virtual修饰,子类使用override和extends修饰,如果需要重写父类的方法,父类中该方法需要用virtual修饰,子类需要使用override。另外如果子类需要使用超类的域或者方法则需要使用super关键字,注意构造方法的复用不需要用成对的virtual和override关键字修饰超类的构造方法和子类的构造方法。
- 利用公式字段插入图片:
IMAGE(path,img_title,height,width)
- 在report中使用
The "Power of One" technique
来统计不重复的数据 - 在Apex中使用静态资源加载jquery:
<apex:page>
<!-- Add the static resource to page's <head> -->
<apex:includeScript value="{! $Resource.jQuery }"/>
<!-- A short bit of jQuery to test it's there -->
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {
jQuery("#message").html("Hello from jQuery!");
});
</script>
<!-- Where the jQuery message will appear -->
<h1 id="message"></h1>
</apex:page>
- 简单的使用vf标准控制器来展示Contact列表,并实现点击名称跳转详细页面。方法有三种:
<apex:page standardController="Contact" recordSetVar="contacts">
<apex:pageBlock title="Contact List">
<apex:repeat value="{!contacts}" var="ct">
<li>
<apex:outputLink value="/{!ct.Id}">{!ct.Name}</apex:outputLink>
<!-- <apex:outputLink value="{!URLFOR($Action.Contact.View,ct.id,[retURL=''])}">{!ct.Name}</apex:outputLink> -->
<!-- <apex:outputLink value="https://componentstest-dev-ed.my.salesforce.com/{!ct.Id}">{!ct.Name}</apex:outputLink> -->
</li>
</apex:repeat>
</apex:pageBlock>
</apex:page>
-
增强lookup查找功能:我们通过lookup进行对父记录Name进行搜索时,通常默认只能使用Name来搜索,有时候比如我们在子记录中想要通过Phone来搜索Account的Name,这个时候可以在setup->enter ‘Search Settings’ in Quick Search Box中即可增强搜索
-
将使用html编辑的前端网站放到force.com平台上的方法:将做好的网站,比如shangpinhui/Bootstrap所有文件打包成zip上传到salesforce的Static Resources中,比如拿shangpinhui为例,目录结构为:shangpinhui->images/js/css/index.html,打包上传后命名为ShangpinhuiZip(名字不能一数字开头),之后在Visualforce Pages中编辑如下代码:
<apex:page docType="html-5.0" sidebar="false" showHeader="false" standardStylesheets="false"
action="{!URLFOR($Resource.ShangpinhuiZip, 'shangpinhui/index.html')}">
</apex:page>
在action里面通过URLFOR表达式来将页面加载进去。这样就不用考虑修改网站页面资源引用的路径了,注意在Developer Edition里面由于每个账号限制只允许放一个网站绑定一个url,所以要实现多个网站同时上传作为作品展示,可以再做一个列表,分别通过超链接映射到相应的网站上,这样就可以将您的所有作品都绑定在一个页面上分别访问。
-
在Company Information中可以查看User Licence的使用次数,如下图:
-
recordSetVar与
<apex:variable value="{!}" var=""/>
的适用场景比较:
recordSetVar保存的是SOBject的List,一般会与<apex:pageBlockTable>
以及<apex:page>
的熟悉standController
(即可以传标准对象也可以传自定义对象)连用,常用于输出性质的组件,而对于输入性质的组件,若强行使用需要加[0],这种场景推荐使用<apex:variable>
标签,来将比较长的api名称用变量存储。 -
15位Id与18位Id的区别?
C:Q:: what is the difference 18 digit id and 15digit?
Ans: When we create record in the object salesforce will create 18 digit unique to recognize it.
This is a case insensitive
Same 18 digit is represented as 15 digit id as case sensitive
Id 001—9000001—1o2Of in this 15/18 digit id first 3digits indicate Object and last 5 digits indicate record
- 两种方法获取组件id:
- {!KaTeX parse error: Expected 'EOF', got '}' at position 17: …omponent.idName}̲ - eg. var el1…Component.name}’).value;// 该方法不需要写嵌套的id,若要指明则:$Component.bk.age
- pg:fm:bk:name - eg. var el2 = document.getElementById(‘pg:fm:bk:name’).value;// 该方法每个组件均需要指明id,并按嵌套关系依次书写,id间以:隔开
上述方法除在js中应用外也可用于css,若不指定id系统自动生成id,并在相对应组件中自动添加嵌套关系。
<apex:page id="pg">
<apex:form id="fm">
<apex:inputText id="name" οnchange="show()"/>
<script>
function show() {
var el1 = document.getElementById('{!$Component.name}').value;
var el2 = document.getElementById('{!$Component.bk.age}').value;
alert('name: '+el1+', age: '+el2);
}
function demo() {
// getting specific dom element by this format, you shold write every level id.
var el1 = document.getElementById('pg:fm:name').value;
var el2 = document.getElementById('pg:fm:bk:age').value;
alert('name: '+el1+', age: '+el2);
}
</script>
<apex:pageBlock id="bk">
<apex:inputText id="age" οnchange="demo()"/>
<apex:pageBlockSection id="bks1">
<apex:inputText id="ip1"/>
</apex:pageBlockSection>
<apex:pageBlockSection id="bks2">
<apex:inputText id="ip2"/>
<apex:outputText id="op"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton value="populateAutomaticly" οnclick="al()" oncomplete="populateAutomaticly()"/>
</apex:pageBlockButtons>
<script>
function populateAutomaticly() {
var el = document.getElementById('{!$Component.bks1.ip1}').value;
document.getElementById('pg:fm:bk:bks2:ip2').value = el;
document.getElementById('pg:fm:bk:bks2:op').innerHTML = el;
}
function al() {
alert('Testing...');
}
</script>
</apex:pageBlock>
</apex:form>
</apex:page>
- apex:page组件与apex:sectionHeader组件属性的相互影响:
apex:page组件中比较少用到的属性:
<!--
setup="true";show sidebar as setup format;
renderAs="pdf";the body you development will show as pdf;
action="someMethods";excute the action first before render the page;
-->
<apex:sectionHeader title="Section Header Practice" subtitle="Home" description="draw your page using code" help="https://baidu.com" printUrl="https://baidu.com"/>
<span style="color:#ff0000">如果page中用到了setup="true"属性,那么sectionHeader中就不能显示对象图标。</span>
- 创建visualforce的4种方法:
- 导航法 - setup->developer->visualforce page;
- developer console;
- IDE - eg. sublime text / eclipes - 安装eclipes并配置forceIDE插件视频 - https://www.youtube.com/watch?v=ufe62nGecMg
- 打开developer mode(两种方式:1. 在User里面-Edit-Development Mode;2. 在My Setting-Personal-Advanced User Details-Edit-Development Mode)后,通过url方式创建。
- 为pageBlockTable中的column组件设置宽度不生效的解决方案:
- 嵌套关系1: form>pageBlock>pageBlockTable>column,可以直接为column增加width的style或者styleClass,需要的话pageBlockTable设置个width:100%;
- 嵌套关系2: form>pageBlock>pageBlockSection>pageBlockTable>column,这个时候添加style和styleClass是不生效的,此时只需要在pageBlockTable中设置columnsWidth=“width1,width2…”;
- 图解批准进程approval process,理解队列,多级审批:http://blog.csdn.net/itsme_web/article/details/732509
- ApexPages.currentPage().getUrl().contains(‘st’),apex查看当前页面url是否包含某个字符串。
- Decimal.valueOf(ApexPages.currentPage().getParameters().get(‘st’)),将字符串形式转换成Decimal格式;
- DateTime.getTime()获取时间戳,单位为ms;
24、使用apex处理日期:
DateTime nowTime = System.now();
String now_date = nowTime.format('yyyy-MM-dd');
String now_dateTime = nowTime.format('yyyy-MM-dd HH:mm:ss');
System.debug(now_date+'<------>'+now_dateTime);
debug信息:
[plain] view plain copy
14:23:00:002 USER_DEBUG [4]|DEBUG|2017-07-26<------>2017-07-26 14:23:00
- 限制点击自定义按钮的简档:
/**
功能说明:判别用户是否为项目比选相关专员
参数说明:用户Id
返回值:true/false
作者:Wilson Xu
日期:2017-07-26
**/
public static Boolean isValidUser(String userId){
Set<String> profileSet = new Set<String>{'品牌专员','事件行销专员','物料制作专员','线上媒介专员','线下媒介专员','展览展示专员','子公司企划专员'};
String profileName = [SELECT Profile.Name FROM User WHERE Id = :userId].Profile.Name;
return profileSet.contains(profileName);
}
// 验证该用户是否为专员简档用户
if(!isValidUser(UserInfo.getUserId())){
result.code = '1';
result.msg = '只有相关专员才能发起定向谈判!';
return JSON.serialize(result);
}
- Apex实例化一个内部类:
public class OuterClass {
public class InnerClass {
String name = '';
Blob body = '';
}
}
// 实例化内部类
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = new OuterClass.InnerClass();
/*接口类实例化*/
global class SyncInterface {
// 封装数据结构 - 返回结果
global class SyncResults{
global List<Response> responses;
}
global class Response{
public String TYPE;
public String MSG;
public String BUSINESS_ID;
public String SALESFORCE_ID;
public String PROC_TYPE;
}
}
// 实例化接口response list
SyncResults syncResponseList = new SyncResults();
syncResponseList.responses = new List<Response>();
- 使用Database.query()查询数据集合:
filterStr = 'AND Type__c = \'Text\' ';// 注意写成转义字符形式
query = 'SELECT Name, Type__c, Message__c, Msgpic__c, Mediaid__c, VioceRecognition__c, VoiceFormat__c, VoiceSrc__c ' +
'FROM BD_CaseDetail__c ' +
'WHERE Case__c = :caseId ' + filterStr + 'ORDER BY CreatedDate ASC LIMIT 500';
caseDetailList = Database.query(query);
caseDetailList = [SELECT Name, Type__c, Message__c, Msgpic__c, Mediaid__c, VioceRecognition__c, VoiceFormat__c, VoiceSrc__c
FROM BD_CaseDetail__c
WHERE Case__c = :caseId AND Type__c = 'Text' ORDER BY CreatedDate ASC LIMIT 500];
-
Live Agent配置:
- 先启用live agent - setup -> live agent settings -> enable
- My Settings -> Advanced settings -> Live Agent User(勾选上)
-
根据自定义地理定位数据类型字段API名表示经纬度:
现有API名称为:Geographic_Coordinates__c的地理位置坐标字段,要表示经度使用:Geographic_Coordinates__Longitude__s, 要表示纬度使用:Geographic_Coordinates__Latitude__s
Select Id, Address__c, Geographic_Coordinates__Longitude__s, Geographic_Coordinates__Latitude__s From Store__c
- 创建Task:
任务:
Task task = new Task();
task.Subject = ‘A信息修改申请';
task.status = 'open';
task.priority = 'High';
task.whatId = '0017F00000CfcdsQAB';// 相关项目,这里是供应商Id
task.ownerId = '0057F000000pe6uQAA';// 被分配人,这里是UserId
// 以下两行是设置提醒
task.IsReminderSet = true;
task.ReminderDateTime = System.now();
insert task;
- Apex中List、Map、Set集合总结:
- List:有序、可重复;
- Map:无序,key重复则value覆盖;
- Set:无序,不可重复;即使重复了,取前面的值,如:
Set<Integer> s = new Set<Integer> {1,2,3,2,1};
system.debug('s: ' + s);
DEBUG INFO:s: {1,2,3}
Sample:
List<Account> accs = [select id, name from account limit 3];
map<Id, Account> m = new map<Id, Account>();
String lastId = '';
if(accs != null && !accs.isEmpty()) {
Integer i = 0;
for(Account a : accs) {
System.debug('a['+i+'] : ' + a);
lastId = a.Id;
m.put(a.Id, a);
i++;
}
}
// 验证List有顺序,Map无顺序
System.debug(m);
System.debug(m.get(lastId));
Map<String, String> m1 = new Map<String, String> {
'key1' => 'value1',
'key2' => 'value2',
'key1' => 'value2',
'key2' => 'value3',
'key1' => 'value3'
};
System.debug('m1: ' +m1);
System.debug('m1.key1: ' + m1.get('key1'));
- Partner User对Quote Tab不可见,Quote无Sharing Rule,它的共享受Opportunity的控制,如果对Partner User的Quote的OLS设置为View且Opportunity页面布局的Quote放出来了,如果共享Opp的Owner没有创建Quote记录,Partner User不可见Quote相关列表,需要创建一条Quote,才可以看见。
- salesforce soql中不能对公式字段使用Group By;
- 封装Map<String, List>技巧 + 去重 | 参考资源:How to Send More than 10 E-mails
Map<Id, List<Case>> userCaseMap = new Map<Id, List<Case>>();
List<Case> allCaseLoggedToday = new List<Case>();
List<Id> salesIds = new List<Id>();
List<User> salesRep = [SELECT Id , Name , Email , ManagerId
FROM User
WHERE Profile.Name = 'System Administrator'];
for(User u : salesRep) {
salesIds.add(u.Id);
}
allCaseLoggedToday = [SELECT Id, CaseNumber,CreatedById, Owner.Name , Account.Name , Contact.Name
FROM Case
WHERE CreatedDate = TODAY AND CreatedById in : salesIds];
for(Case c : allCaseLoggedToday) {
if(userCaseMap.containsKey(c.CreatedById)) {
//Fetch the list of case and add the new case in it
List<Case> tempList = userCaseMap.get(c.CreatedById);
tempList.add(c);
//Putting the refreshed case list in map
userCaseMap.put(c.CreatedById , tempList);
}else {
//Creating a list of case and outting it in map
userCaseMap.put(c.CreatedById , new List<Case>{c});
}
}
- 使用apex获取IP地址:ApexPages.currentPage().getHeaders().get(‘X-Salesforce-SIP’);
- 在为输入元素赋初始值时,我们经常会使用url将参数写入该输入元素的Name属性,我们通常会担心,在Org迁移时,这些Name值是不是会改变,从而增加我们的维护负担?经调研,Vf画面的Name不会随着Org的改变而改变。
- Salesforce获取某个日期所在月份的天数:
Date d = System.today();
Integer numberDays = date.daysInMonth(d.Year(), d.Month());
System.debug(numberDays);
-
Salesforce布局特性:如果相关列表上无任何按钮,那么相关列表面板只有在有记录时才会显示。
-
Lead Home Page调整经验之谈:
- 没有配置方法实现Home页面布局调整,比如将Tools和Reports换个位置;
- 无法用自定义报表链接替换Reports里面的标准报表链接-无法编辑标准报表,无法将自定义报表移至Lead Reports Folder;
- 如果必须实现只能重写标准Lead Tab按钮;
- 如果记录锁定,用户需要更新记录,则必须使用Webservice,比如:报价锁定非系统管理员需自动同步报价时,我们需要在Trigger里面调用Webservice,这时就需要使用到@future异步方式了。
- Opportunity和Quote为Master-Detail关系,在导入历史数据时,Opportunity和Quote的Owner未同步,事后同步时,不可使用apex方法更新Quote Owner,错误信息为:“Can’t change quote owner. Enable quotes without parent opportunities.”。
- System.debug('result: ’ + rtMap.get(myRt).equals(qt.RecordTypeId) + '–qt.RecordTypeId: ’ + qt.RecordTypeId + '–rtMap.get(myRt): ’ + rtMap.get(myRt));Id为18位。
- 去重Sample - 看似怪诞,实则大有用处:
Map<String, String> accIDs = new Map<String,String>();
for(Quote qt: (List<Quote>)Trigger.New){
if(!accIDs.containsKey(qt.Quote_To_Account__c))
accIDs.put(qt.Quote_To_Account__c,'');
}
Map<String, Account> accs = new Map<String, Account>([SELECT id,BillingStreet,BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id IN : accIDs.keySet()]);
- 启用或禁用内联编辑 - 有时候我们希望禁用用户默认的行内编辑功能,直接去user interface中将Enable Inline Editing关掉即可。
- 问题:System Administrator的Profile中Home的Tab Settings显示Default On,在Tab面板和Tab的customize列表中并没有看到Home?- 先禁用profile的enhanced profile user interface,之后在profile的Tab settings中勾上下图1,保存即可。参见资料:传送门
- 在apex中启用记录锁来锁定或解锁记录。
select count() from DTT__Geographic_Region__c
select count() from DTT__Geographic_Region__c
select count() from DTT__Geographic_Region__c
select count() from DTT__Geographic_Region__c
select count() from DTT__Geographic_Region__c
-
如何配置实现超级管理员下次登录salesforce org不需要输入验证码:Profile -> System Administrator -> Login IP Ranges,设置IP Start Address:0.0.0.0,IP End Address:255.255.255.255即可。
-
使用Mavensmate同步DEV与UAT环境中简档的字段级别权限:在环境迁移时,部分元数据会存在丢失,导致两环境存在差异,比如简档中档FLS。
- 【Step by Step】:先登陆到DEV环境,利用Mavensmate retrieve Profile和Custom Object(标准对象也在里面),然后勾选某对象如Order的fields和简档明细,点击Update后,在Sublime中打开文件;使用同样档方法将UAT相关文件下载到本地,然后直接复制粘贴简档文件档元数据覆盖UAT即可。
- 【使用soql查询记录条数】:
select count() from DTT__Geographic_Region__c
- 【获取Label值】:
{!$ObjectType.ACC_Return_Order_Product__c.LabelPlural}
{!$ObjectType.ACC_Return_Order_Product__c.Fields.ACC_Product__c.Label}
- 关于Datetime.now(的)日期操作:传送门,在查询语句中获取上次修改时间:
SELECT Id,Name,OwnerId,AccountId,LastModifiedDate FROM Opportunity
- 查询15分钟内修改的记录:
select name from Opportunity where SystemModstamp > :Datetime.now().addMinutes(-15)
- 用LAST_N_DAYS:天数,来查询据当前时间间隔多少天的数据:
SELECT Id,Name,OwnerId,AccountId,LastModifiedDate FROM Opportunity WHERE StageName != '签订合同' AND StageName != '终止' AND LastModifiedDate = LAST_N_DAYS:30