Salesforce 开发整理(九) 开发中使用的一些小技巧汇总[持续更新]

1.查询一个对象下所有字段
当需要查询一个对象所有字段进行复制或其他操作,可以使用一段拼接的语句来查询
1 String query = 'select ';
2 for(String fieldApi :  Schema.SobjectType.Opportunity.fields.getMap().keySet()){
3         if(fieldApi=='Id')
4           continue;
5         query += fieldApi + ', ';
6 }
7 query += 'Id from Opportunity';
8 System.debug(query);

2.获取记录类型的几种方式

 1 //第一种
 2 String recordType = Schema.SObjectType.Good__c.getRecordTypeInfosByName().get('中端品牌').getRecordTypeId();
 3 system.debug('第一种:' + recordType);
 4 //第二种:
 5 List<RecordType> list_type = [Select id,Name,IsActive,DeveloperName  FROM RecordType  where Name = '奢侈品牌' and  IsActive  = true];
 6 System.debug('第二种:' + list_type);
 7 
 8 //第三种
 9 List<RecordType> list_type3=[select Id,DeveloperName,Name from RecordType where (DeveloperName='MiddleBrand' OR DeveloperName='extravagant') 
10           AND SObjectType='Good__c'];
11 System.debug('第三种:' + list_type3);
12           
13 //第四种
14 List<RecordType> list_type2 = [Select Id,Name,DeveloperName From RecordType where sobjecttype = 'Good__c'];
15 System.debug('第四种:' + list_type2);
16 
17 //************************************************************************************************************
18 
19 /*********
20     *
21     *    @function
22     *        传入对象名,获取一个Map<对象记录类型id,对象记录类型名>
23     *
24     *
25     */
26 public static Map<ID,RecordType> getObjectRecordType(String objectName){
27     
28     
29     Map<ID,RecordType> RecordTypeMap = new Map<ID,RecordType>([
30                                 SELECT 
31                                    Id,DeveloperName
32                                 FROM 
33                                    RecordType
34                                 WHERE 
35                                    SObjectType =: objectName
36                                 ]);
37     
38     return RecordTypeMap;
39 }   
 
3.List<sobject>与JSON串的转换
1 String json_String = JSON.serialize(List<Opportunity> list_object);
2 List<Opportunity>)JSON.deserialize(String json_String, List<Opportunity>.class);

提供一个工具网站,将JSON自动转换成Apex类:JSON to Apex

4.BASE64位与MD5加密

1 // base64Encode:base64编码
2 String AccountId = 'X66666694292';
3 String mytime = Datetime.now().format('yyyyMMddHHmmss');
4 String authorizationHeader = EncodingUtil.base64Encode(Blob.valueOf(AccountId + ':' + mytime));
5 System.debug('authorizationHeader:' + authorizationHeader);
6 
7 //sig的值为 32位大写MD5加密 (帐号Id + 帐号APISecret +时间戳)
8 String sig = AccountId + APISecret + mytime;
9 String token = EncodingUtil.convertToHex(Crypto.generateDigest('MD5', Blob.valueOf(sig))).toUpperCase();

5.订单产品页面布局调整

调整标准的订单产品添加页面字段,调整后预览如下

修改订单产品页面布局右上方下的多行式项目布局即可

6.自定义标签提示错误信息

通过自定义标签创建一条提示消息

然后在trigger里面判断,如果不满足条件可以抛出这条错误消息。效果如下图

 对应的代码,其实可以看到提示消息就是一个字符串,但是使用自定义标签能够支持配置提示消息。

1 trigger OpportunityTrigger on Opportunity (after insert) {
2  
3      if(trigger.isInsert && trigger.isAfter){
4          for(Opportunity opp:trigger.new){
5              opp.addError(Label.Opp_Machine_Approval); 
6          }
7      }
8 }

7.自定义设置没有列表

在方案设置中开启列表类型

开启后预览效果

8.生成随机数

发现Salesforce没有比较顺手的随机数生成方法,自己写了一个功能函数备用

1 // @size 0-size范围的随机数 
2 public static Integer getRandomNumber(Integer size){ 
3     return ((math.random()) * size).intValue();
4 }
5 
6 // @size 【lowerValue,upperValue】 范围的随机数
7 public static Integer getRandomNumber(Integer lowerValue,Integer upperValue){  
8     return (math.random() * (upperValue - lowerValue + 1 ) + lowerValue).intValue();
9 }

9.自定义提交待审批按钮

按钮实现JS代码

 1 {!REQUIRESCRIPT("/soap/ajax/35.0/connection.js")}
 2 {!REQUIRESCRIPT("/soap/ajax/35.0/apex.js")}
 3 
 4 var recordtype = '{!Opportunity.RecordType}';
 5 var status = '{!Opportunity.ApprovalStatus__c}';// 审批状态
 6 var stage = '{!Opportunity.StageName}';
 7 
 8 if(recordtype != '需要的记录类型'){     
 9     alert('当前业务机会记录类型为:' + recordtype + '不能使用该审批!');
10 }else if(status == '审批中'){
11     alert('当前业务机会正在审批中,请耐心等待审批结果!');
12 }else if(status == '已通过'){
13     alert('当前业务机会已审批通过,请不要重复提交');
14 }else{
15     var request = new sforce.ProcessSubmitRequest();
16     request.objectId = "{!Opportunity.Id}";
17     var processRes = sforce.connection.process([request]);
18     if(processRes[0].getBoolean('success')){
19         alert("已提交报价审批,请等待审批完成!");
20         window.location.reload();
21     }else{
22         alert("提交审批错误:" + processRes[0].errors.message);
23     }
24 }

需要注意的,是保证对审批流条件的控制,虽然不做控制系统也会自动识别该选择那条审批流,但是这样没办法友好的提示用户有那些条件是审批必须要满足的

猜你喜欢

转载自www.cnblogs.com/luqinghua/p/9349526.html