1 获取记录类型的几种方式
//第一种 推荐使用,因为不需要sql查询
String recordType = Schema.SObjectType.Good__c.getRecordTypeInfosByName().get('中端品牌').getRecordTypeId();
system.debug('第一种:' + recordType);
//第二种:
List<RecordType> list_type = [Select id,Name,IsActive,DeveloperName FROM RecordType where Name = '奢侈品牌' and IsActive = true];
System.debug('第二种:' + list_type);
//第三种
List<RecordType> list_type3=[select Id,DeveloperName,Name from RecordType where (DeveloperName='MiddleBrand' OR DeveloperName='extravagant')
AND SObjectType='Good__c'];
System.debug('第三种:' + list_type3);
//第四种
List<RecordType> list_type2 = [Select Id,Name,DeveloperName From RecordType where sobjecttype = 'Good__c'];
System.debug('第四种:' + list_type2);
2 List与JSON串的转换
String json_String = JSON.serialize(List<Opportunity> list_object);
List<Opportunity>)JSON.deserialize(String json_String, List<Opportunity>.class);
3 BASE64位与MD5加密
// base64Encode:base64编码
String AccountId = 'X66666694292';
String mytime = Datetime.now().format('yyyyMMddHHmmss');
String authorizationHeader = EncodingUtil.base64Encode(Blob.valueOf(AccountId + ':' + mytime));
System.debug('authorizationHeader:' + authorizationHeader);
//sig的值为 32位大写MD5加密 (帐号Id + 帐号APISecret +时间戳)
String sig = AccountId + APISecret + mytime;
String token = EncodingUtil.convertToHex(Crypto.generateDigest('MD5', Blob.valueOf(sig))).toUpperCase();
4 获取所有字段的sql
SELECT FIELDS(ALL) FROM Vehicle__c LIMIT 5
OR
String query = 'select ';
for(String fieldApi : Schema.SobjectType.Opportunity.fields.getMap().keySet()){
if(fieldApi=='Id')
continue;
query += fieldApi + ', ';
}
query += 'Id from Opportunity';
System.debug(query);
5 指定时间执行定时任务
private void createNewSchedule(){
SyncFromSapToSfSchedule sc = new SyncFromSapToSfSchedule();
System_Setting__c ssc = System_Setting__c.getValues('Setting');
Datetime dt = Datetime.now().addMinutes(Integer.valueOf(ssc.SAP_Interval_Minutes__c));
List<String> args = new List<String>();
args.add(String.valueOf(dt.minute()));
args.add(String.valueOf(dt.hour()));
args.add(String.valueOf(dt.day()));
args.add(String.valueOf(dt.month()));
args.add(String.valueOf(dt.year()));
String fmt = String.format('30 {0} {1} {2} {3} ? {4}',args);//指定时间执行一次
System.debug('*** 执行时间:' + fmt);
Id sid = System.schedule('SAP同步数据到SF_'+ dt.format('yyMMddHHmmss.SSS'), fmt, sc);
}
6 停止正在运行batch
//如果batch进入了死循环,最好就是修改代码,修改条件,停止batch
for ( AsyncApexJob aJob : [ Select id ,Status from AsyncApexJob where Status = 'Queued' or Status='holding'] )
{
System.AbortJob(aJob.Id);
}
for(CronTrigger job : [SELECT Id FROM CronTrigger WHERE id='08e0T000002XaGoQAK']){
System.debug('job.Id+++++++++' +job.Id);
System.abortJob(job.Id);
}
7 salesforce 中自动发送的邮件
1. Process Automation Settings
//在这里配置salesforce的发件人邮箱
2. Organization-Wide Email Addresses
8 生成随机数
// @size 0-size范围的随机数
public static Integer getRandomNumber(Integer size){
return ((math.random()) * size).intValue();
}
// @size 【lowerValue,upperValue】 范围的随机数
public static Integer getRandomNumber(Integer lowerValue,Integer upperValue){
return (math.random() * (upperValue - lowerValue + 1 ) + lowerValue).intValue();
}
9 自定义提交待审批按钮
//按钮实现JS代码
{!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/35.0/apex.js")}
var recordtype = '{!Opportunity.RecordType}';
var status = '{!Opportunity.ApprovalStatus__c}';// 审批状态
var stage = '{!Opportunity.StageName}';
if(recordtype != '需要的记录类型'){
alert('当前业务机会记录类型为:' + recordtype + '不能使用该审批!');
}else if(status == '审批中'){
alert('当前业务机会正在审批中,请耐心等待审批结果!');
}else if(status == '已通过'){
alert('当前业务机会已审批通过,请不要重复提交');
}else{
var request = new sforce.ProcessSubmitRequest();
request.objectId = "{!Opportunity.Id}";
var processRes = sforce.connection.process([request]);
if(processRes[0].getBoolean('success')){
alert("已提交报价审批,请等待审批完成!");
window.location.reload();
}else{
alert("提交审批错误:" + processRes[0].errors.message);
}
}