博主也是Salesforce Apex 工作不久,有用到的知识和平时查阅到的知识和大家分享一下。
1、使用SOQL语句查询时,字符串类型的只能使用‘单引号’,否则报错:Unknown error parsing query;
eg:SELECT Id, Subject, CaseNumber, Status, Priority
FROM Case
WHERE CaseNumber = '00001036' //注意此处autoNumber类型数字为String类型
使用SOQL其他注意事项:
eg:Case标准对象的Subject API Name即为 Subject
eg:TODAY() - DATEVALUE( CreatedDate )
Implementd__Date__c - DATEVALUE( CreatedDate )
注意:只有当日期为标准字段时,才使用DATEVALUE()来转化
String sessionID = UserInfo.getSessionId();
System.debug(sessionID);
@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 params = (Map)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后赋值给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)
The "Power of One" technique
来统计不重复的数据
{!ct.Name}
增强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中编辑如下代码:
在action里面通过URLFOR表达式来将页面加载进去。这样就不用考虑修改网站页面资源引用的路径了,注意在Developer Edition里面由于每个账号限制只允许放一个网站绑定一个url,所以要实现多个网站同时上传作为作品展示,可以再做一个列表,分别通过超链接映射到相应的网站上,这样就可以将您的所有作品都绑定在一个页面上分别访问。
在Company Information中可以查看User Licence的使用次数,如下图:
recordSetVar与
的适用场景比较:
recordSetVar保存的是SOBject的List,一般会与
以及
的熟悉standController
(即可以传标准对象也可以传自定义对象)连用,常用于输出性质的组件,而对于输入性质的组件,若强行使用需要加[0],这种场景推荐使用
标签,来将比较长的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
如果page中用到了setup="true"属性,那么sectionHeader中就不能显示对象图标。
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 profileSet = new Set{'品牌专员','事件行销专员','物料制作专员','线上媒介专员','线下媒介专员','展览展示专员','子公司企划专员'};
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);
}
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 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();
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配置:
根据自定义地理定位数据类型字段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 = 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;
Set s = new Set {1,2,3,2,1};
system.debug('s: ' + s);
DEBUG INFO:s: {1,2,3}
Sample:
List accs = [select id, name from account limit 3];
map m = new map();
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 m1 = new Map {
'key1' => 'value1',
'key2' => 'value2',
'key1' => 'value2',
'key2' => 'value3',
'key1' => 'value3'
};
System.debug('m1: ' +m1);
System.debug('m1.key1: ' + m1.get('key1'));
Map> userCaseMap = new Map>();
List allCaseLoggedToday = new List();
List salesIds = new List();
List 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 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{c});
}
}
Date d = System.today();
Integer numberDays = date.daysInMonth(d.Year(), d.Month());
System.debug(numberDays);
Salesforce布局特性:如果相关列表上无任何按钮,那么相关列表面板只有在有记录时才会显示。
Lead Home Page调整经验之谈:
Map accIDs = new Map();
for(Quote qt: (List)Trigger.New){
if(!accIDs.containsKey(qt.Quote_To_Account__c))
accIDs.put(qt.Quote_To_Account__c,'');
}
Map accs = new Map([SELECT id,BillingStreet,BillingCity, BillingState, BillingPostalCode, BillingCountry FROM Account WHERE Id IN : accIDs.keySet()]);
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。
select count() from DTT__Geographic_Region__c
{!$ObjectType.ACC_Return_Order_Product__c.LabelPlural}
{!$ObjectType.ACC_Return_Order_Product__c.Fields.ACC_Product__c.Label}
SELECT Id,Name,OwnerId,AccountId,LastModifiedDate FROM Opportunity
select name from Opportunity where SystemModstamp > :Datetime.now().addMinutes(-15)
SELECT Id,Name,OwnerId,AccountId,LastModifiedDate FROM Opportunity WHERE StageName != '签订合同' AND StageName != '终止' AND LastModifiedDate = LAST_N_DAYS:30