1.SOQL中,字符串类型只能用单引号括起来,否则会报错:Unknown error parsing query.
2.标准字段的API NAME和Field NAME相同。
3.计算两个日期间隔天数:TODAY - DATEVALUE(CreatedDate)
注:只有当日期为标准字段时,才能使用DATEVALUE()来转化。
4.获取Session ID的方法:
String sessionID = UserInfo.getSessionId();
5.完整版的REST services demo:
@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager{
@HttpGet
global static Case getCaseById(){
RestRequest req = RestContext.request;
String caseId = req.requestURL.substring(req.requestURI.lastIndexOf('/') + 1);
Case result = [SELECT CaseNumber, Subject, Status FROM Case WHERE Id = :caseId];
return result;
}
@HttpPost
global static ID createCase(String subject, String status){
Case thisCase = new Case(
Subject = subject,
Status = status);
insert thisCase;
return thisCase.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;
}
@HttpPut
global static void upsertCase(String id,String subject, String status){
Case thisCase = new Case(
Id = id,
Subject = subject,
Status = status
);
upsert thisCase;
return thisCase.Id;
}
@HttpPatch
global static ID updateCase(){
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;
}
}
注:(1)所有的方法类型都须是global static的。
(2)HttpPut和HttpPost分别对应upsert和insert。
6.Apex在继承时需要用到的关键字:extends、super、virtual、override
(1)父类必须使用virtual来继承,子类使用override和extends修饰;
(2)如果要重写父类的方法,父类中该方法要用virtual修饰,子类要用override修饰;
(3)如果子类需要使用父类的域或者方法,则需要使用super关键字。
7.利用公式(formula)插入图片:IMAGE(path,img_title,height,width)
8.在report中使用【The "Power of One"】来统计不重复的数据。
9.在Apex中使用静态资源加载jquery的代码:
<apex:page>
<apex:includeScript value="{!$Resource.jQuery}"/>
<script type="text/javascript">
jQuery.noConflict();
JQuery(document).ready(function(){
jQuery("#message").html("Hello!");
});
</script>
<h1 id="message"></h1>
</apex:page>
10.用visualforce page标准控制器来展示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=''])}"></apex:outputLink> -->
<!-- <apex:outputLink value="https://test.salesforce.com/"></apex:outputLink> -->
</li>
</apex:repeat>
</apex:pageBlock>
</apex:page>
11.在【设定】→【检索设定】中可以使用增强检索
12.将html编辑的前端网站放到force.com平台的方法:
(1)将前端网站的代码打成zip包,上传到【静的なリソース】里面
(2)在visual force中使用下面的代码进行引用:
<apex:page docType="html-5.0" sidebar="false" showHeader="false" standardStylesheets="false" action="{!URLFOR($Resource.codeZip),'codeZip/index.html'}">
</apex:page>
13.查看当前页面URL是否包含“st”字符串:
ApexPages.currentPage().getUrl().contains('st');
14.将字符串转为Decimal格式:
Decimal.valueOf('string');
15.获取时间戳(单位ms):
DateTime.getTime();
16.String转Date:
Date myDate = date.valueOf('2022-2-2');
17.使用Apex处理日期:
DateTime nowTime = System.now();
String now_date = nowTime.format('yyyy-MM-dd');
String now_dateTime = nowTime.format('yyyy-MM-dd HH:mm:ss');
18.禁止某些Profile的用户点击自定按钮的代码:
public static Boolean isValidUser(String userId){
Set<String> profileSet = new Set<String>{'Profile1','Profile2','Profile3'};
String profileName = [SELECT Profile.Name FROM WHERE Id = :userId].Profile.Name;
return profileSet.contains(profileName);
}
//Validate
if(!isValidUser(UserInfo.getUserId())){
result.code = '1';
result.msg = 'No Permission!';
return JSON.serialize(result);
}
19.在Apex中实例化一个内部类:
public class OuterClass{
public class InnerClass{
String name = '';
Blob name = '';
}
}
OuterClass outer = new OuterClass();
OuterClass.InnerClass inner = new OuterClass.InnerClass();
global class SyncInterface{
global List<Response> responses;
}
global class Response{
public String TYPE;
public String MSG;
}
SyncResults syncResponseList = new SyncResults();
syncResponseList.responses = new List<Response>();
20.Apex中查询数据的方法:
(1)使用Database.query()方法:
String str = 'AND Type__c = \'Text\'';//转义
String nameStr = 'test';
String soqlStr = 'SELECT Name,Type__c from Info__c WHERE Name = :nameStr' + str +'ORDER BY Type__c';
List<Info__c> infoList = Database.query(soqlStr);
(2)使用soql语句直接查询:
String name = 'test';
List<Info__c> infoList = [select name from Info__c WHERE name = :name AND Type__c = 'Text' ORDER BY name];
21.Apex中的List、Map、Set集合:
(1)List:有序、可重复
(2)Map:无序
(3)Set:无序、不可重复。重复的场合,取旧值。
Set<Integer> s = new Set<Integer>{1,2,3,2,1};
System.debug(s); //{1,2,3}
22.封装Map<String,List<Sobject>>技巧+去重:
Map<Id,List<Case>> case1 = new Map<Id,List<Case>>();
List<Case> case2 = new List<Case>();
List<Id> idInfo = new List<Id>();
List<User> userInfo = [SELECT Id,name,Email FROM User WHERE Profile.Name = 'System Administrator'];
for(User u : userInfo){
idInfo.add(u.Id);
}
case2 = [SELECT Id, CaseNumber FROM Case WHERE CreatedDate = TODAY];
for(Case c : case2){
if(case1.containsKey(c.CreatedById)){
List<Case> tempList = case1.get(c.CreatedById);
tempList.add(c);
case1.put(c.CreatedById,tempList);
}else{
case1.put(c.CreatedById , new List<Case>{c});
}
}