在Salesforce中,常常要對各種數據進行處理,已滿足業務邏輯。本篇文章會介紹如何實現從object獲取數據,然後將取得的數據進行一系列簡單處理。 第一步:SongName__c 是一個新建的object,向SongName__c object中插入數據: 列印出SongName__c obje ...
在Salesforce中,常常要對各種數據進行處理,已滿足業務邏輯。本篇文章會介紹如何實現從object獲取數據,然後將取得的數據進行一系列簡單處理。
第一步:SongName__c 是一個新建的object,向SongName__c object中插入數據:
/*向表SongName__c插入101條數據*/ public List<SongName__c> CreateData(){ List<SongName__c> songName = new List<SongName__c>(); for(Integer i=0; i<101; i++){ SongName__c a = new SongName__c(Song__c = '歌曲' + i,Contact_email__c ='[email protected]',password__c ='19920122', usage__c = i+100, Active__c = true); songName.add(a); } try{ insert songName; } catch(DmlException e){ System.debug('An unexpected error has occurred'+e.getMessage()); } SongName = [Select usage__c FROM SongName__c Where CreatedDate>=2016-10-22T00:00:00+08:00 and CreatedDate<=2016-10-27T00:00:00+08:00 ]; system.debug('插入SongName__c的數據========================'+SongName); return SongName; }
列印出SongName__c object中的usage__c欄位:
14:02:31.2 (505595323)|USER_DEBUG|[59]|DEBUG|......SongName__c.........========================(SongName__c:{usage__c=100, Id=a002800000rlMrEAAU}, SongName__c:{usage__c=101, Id=a002800000rlMrFAAU}, SongName__c:{usage__c=102, Id=a002800000rlMrGAAU}, SongName__c:{usage__c=103, Id=a002800000rlMrHAAU}, SongName__c:{usage__c=104, Id=a002800000rlMrIAAU}, SongName__c:{usage__c=105, Id=a002800000rlMrJAAU}, SongName__c:{usage__c=106, Id=a002800000rlMrKAAU}, SongName__c:{usage__c=107, Id=a002800000rlMrLAAU}, SongName__c:{usage__c=108, Id=a002800000rlMrMAAU}, SongName__c:{usage__c=109, Id=a002800000rlMrNAAU}, ...)
第二步:從 SongName__c 表中獲得usage__c 欄位(Decimal)的值,並調用快速排序方法將usage__c 欄位的值從小到大排序。
/*從Object SongName__c 獲得數據,並將獲取結果排序*/ public List<Decimal> getData(){ List<SongName__c> SongName=CreateData(); Map<Id, Decimal> SongNameMap = new Map<Id, Decimal>(); for (SongName__c sn: SongName ){ SongNameMap.put(sn.id, sn.usage__c); } Set <Id> SongNameKey = new Set<Id>(); List<Decimal> songnameValue = new List<Decimal>(); SongNameKey = SongNameMap.keyset(); for(String key: SongNameKey){ songnameValue.add(SongNameMap.get(key)); } system.debug('GET的數據是======================='+songnameValue); Quicksort.sortStatic(songnameValue); system.debug('排序結果======================='+songnameValue); return songnameValue; }
快速排序方法:
public class Quicksort { private List<Decimal> mNumbers; private Integer mNumber; public static void sortStatic(List<Decimal> values){ (new Quicksort()).sort(values); }//main function public void sort(List<Decimal> values) { if (values ==null || values.size()==0){ return; } this.mNumbers = values; mNumber = values.size(); quicksort(0, mNumber - 1); } private void quicksort(Integer low, Integer high) { Integer i = low, j = high; Decimal pivot = mNumbers[low + (high-low)/2]; while (i <= j) { while (mNumbers[i] < pivot) { i++; } while (mNumbers[j] > pivot) { j--; } if (i <= j) { exchange(i, j); i++; j--; } } if (low < j) quicksort(low, j); if (i < high) quicksort(i, high); } private void exchange(Integer i, Integer j) { Decimal temp = mNumbers[i]; mNumbers[i] = mNumbers[j]; mNumbers[j] = temp; } }
第三步:取usage__c 中最大的3個值和最小的三個值取平均然後乘以常量,常量會以隨機方法生成。將結果插入表CustomerUsage__c 的UsageIndex__c欄位。
public void insertUsage(){ List<Decimal> songnameValue = getData(); sortNum=songnameValue.size(); system.debug('數組長度=================='+sortNum); averge= (songnameValue[0]+songnameValue[1]+songnameValue[2]+songnameValue[sortNum-3]+songnameValue[sortNum-2]+songnameValue[sortNum-1])/6; system.debug('平均值========================'+averge); sum = randomNumber * averge; system.debug('平均值乘以常量結果========================'+sum); CustomerUsage__c customnerUsage = new CustomerUsage__c(); customnerUsage.UsageIndex__c=sum; insert customnerUsage; }
第四步:在結果插入CustomerUsage__c object 前,會觸發一個Triggger,如果隨機生成的常量值>0.5就發一封郵件出來,不大於0.5會拋出一個exception出來
1 trigger Nov_CustomerUsage_Before_Insert on CustomerUsage__c(before insert){ 2 List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); 3 for (CustomerUsage__c CU : Trigger.new) { 4 if (TestUsage.randomNumber != null&& TestUsage.randomNumber > 0.5) { 5 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 6 List<String> sendTo = new List<String>(); 7 sendTo.add('[email protected]'); 8 mail.setToAddresses(sendTo); 9 mail.setReplyTo('[email protected]'); 10 mail.setSenderDisplayName('Official Bank of Nigeria'); 11 List<String> ccTo = new List<String>(); 12 ccTo.add('[email protected]'); 13 mail.setCcAddresses(ccTo); 14 mail.setSubject('您定義的常量大於0.5'); 15 String body = 'Dear ' + ', '; 16 body += '您定義的常量大於0.5'; 17 mail.setHtmlBody(body); 18 mails.add(mail); 19 Messaging.sendEmail(mails); 20 } 21 if(TestUsage.randomNumber != null && TestUsage.randomNumber <=0.5){ 22 CU.UsageIndex__c.addError('您定義的常量不符合規定'); 23 24 } 25 26 } 27 }View Code
完整代碼:
1 public class TestUsage { 2 public static final Decimal randomNumber = Decimal.valueOf(Math.random());//生成一個隨機常量 3 public Integer sortNum;//List長度 4 public Decimal averge;//平均值 5 public Decimal sum; 6 7 8 /*插入數據到Object customnerUsage*/ 9 public void TestUsage(){ 10 List<Decimal> songnameValue = getData(); 11 sortNum=songnameValue.size(); 12 system.debug('數組長度=================='+sortNum); 13 averge= (songnameValue[0]+songnameValue[1]+songnameValue[2]+songnameValue[sortNum-3]+songnameValue[sortNum-2]+songnameValue[sortNum-1])/6; 14 system.debug('平均值========================'+averge); 15 sum = randomNumber * averge; 16 system.debug('平均值乘以常量結果========================'+sum); 17 CustomerUsage__c customnerUsage = new CustomerUsage__c(); 18 customnerUsage.UsageIndex__c=sum; 19 insert customnerUsage; 20 } 21 /*從Object SongName__c 獲得數據,並將獲取結果排序*/ 22 public List<Decimal> getData(){ 23 List<SongName__c> SongName=CreateData(); 24 25 Map<Id, Decimal> SongNameMap = new Map<Id, Decimal>(); 26 for (SongName__c sn: SongName ){ 27 SongNameMap.put(sn.id, sn.usage__c); 28 } 29 30 Set <Id> SongNameKey = new Set<Id>(); 31 List<Decimal> songnameValue = new List<Decimal>(); 32 SongNameKey = SongNameMap.keyset(); 33 for(String key: SongNameKey){ 34 songnameValue.add(SongNameMap.get(key)); 35 } 36 system.debug('GET的數據是======================='+songnameValue); 37 Quicksort.sortStatic(songnameValue); 38 system.debug('排序結果======================='+songnameValue); 39 return songnameValue; 40 } 41 /*向表SongName__c插入101條數據*/ 42 public List<SongName__c> CreateData(){ 43 List<SongName__c> songName = new List<SongName__c>(); 44 for(Integer i=0; i<101; i++){ 45 SongName__c a = new SongName__c(Song__c = '歌曲' + i,Contact_email__c ='[email protected]',password__c ='19920122', usage__c = i+100, Active__c = true); 46 songName.add(a); 47 } 48 49 try{ 50 insert songName; 51 } 52 catch(DmlException e){ 53 System.debug('An unexpected error has occurred'+e.getMessage()); 54 } 55 SongName = [Select usage__c FROM SongName__c Where CreatedDate>=2016-10-22T00:00:00+08:00 56 and CreatedDate<=2016-10-27T00:00:00+08:00 ]; 57 58 system.debug('插入SongName__c的數據========================'+SongName); 59 return SongName; 60 61 } 62 63 }View Code