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 }

需要注意的,是保證對審批流條件的控制,雖然不做控制系統也會自動識別該選擇那條審批流,但是這樣沒辦法友好的提示用戶有那些條件是審批必須要滿足的

 10 刪除Chatter數據

1 List<FeedItem> list_feed = new List<FeedItem>([select id from FeedItem]);
2 delete list_feed;

 11 正則表達式拆分中英文

1 String str = '123中文英文Englist通過正則@#!!進行&^%拆分';
2 System.debug('規格型號:' + str.replaceAll('[\u4E00-\u9FA5]',' '));
3 System.debug('名稱:' + str.replaceAll('[^\u4e00-\u9fa5]',''));

拆分后預覽

12 事件不超過24H限制解除

錯誤消息:

Duration must be between 0 and 1440 minutes - Salesforce Error - [FIELD_INTEGRITY_EXCEPTION]

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM