Friday, 13 December 2013

Salesforce Comparable Interface Using It to Sort Wrapper Class

Salesforce have an Interface named as "Comparable" , it is used to add List sorting support for your Apex class

To know more about the Comparable click on this link Comparable Interface

So I used it to compare created date of my history Account records , I have a picklist name Grade__c on Account, whenever user change it , saleforce created a history record for that change.

Note : You need to activate the history tracking for Account and for that field too, for which you want history tracking.

So now am going to tell you how my code works,

Whenever user Updates OR change value of Grade on Account object it will be added on the report chart of history

Here is the snapshot of that report chart


For that I have created a main class and as well as a wrapper class

Wrapper Class
 /**  
 * Description : Controller class for page of Account History of Grade  
 *  
 * Created Date : 12/10/2013  
 *  
 * Created By : Abhi Tripathi  
 *  
 * Version : V1.0   
 **/  
 global class AccountHistoryWrapper implements Comparable{  
   public Account account {get; set;}  
   public List<AccountHistory> accountHistories {get; set;}  
   //Calling Constructor  
   global AccountHistoryWrapper(Account account, List<AccountHistory> accountHistories) {  
     this.account = account;  
     this.accountHistories = accountHistories;   
   }  
   // Compare opportunities based on the opportunity amount.  
   global Integer compareTo(Object compareTo) {  
     // Cast argument to AccountHistoryWrapper  
     AccountHistoryWrapper aHW = (AccountHistoryWrapper)compareTo;  
     // The return value of 0 indicates that both elements are equal.  
     Integer returnValue = 0;  
     if ( aHW.account.CreatedDate > aHW.account.CreatedDate) {   
       // Set return value to a positive value.  
       returnValue = 1;  
     } else if ( aHW.account.CreatedDate < aHW.account.CreatedDate) {  
       // Set return value to a negative value.   
       returnValue = -1;   
     }  
     return returnValue;   
   }  
 }  

Here is the Controller Class

 /**   
 * Description : Controller class for page of Account History of Grade   
 *   
 * Created Date : 12/10/2013   
 *   
 * Created By : Abhi Tripathi   
 *   
 * Version : V1.0    
 **/   
 public class AccountHistoryController {   
   public List<AccountHistoryWrapper> accountHistoriesWrapList {get; set;}   
   //Calling cosnturctor   
   public AccountHistoryController() {   
     //Memory Allocation   
     accountHistoriesWrapList = new List<AccountHistoryWrapper>();   
     //Loop through Accounts   
     for(Account acc : [Select Id, Name, CreatedDate, (Select ID, Field, AccountId, CreatedDate, Createdby.Name, Oldvalue,    
                              Newvalue, Account.Name From Account.Histories   
                              Where Field = 'Grade__c' ORDER BY CreatedDate DESC LIMIT 200 )   
               FROM Account ORDER BY LastModifiedDate DESC]) {    
                 //Populate wrapper list with values   
                 if(acc.Histories.size() > 0)   
                   accountHistoriesWrapList.add(new AccountHistoryWrapper(acc, acc.Histories));    
               }   
     //Get List of wrapper and sort it   
     accountHistoriesWrapList.sort();    
   }   
 }   

Here is the Page

 <!--   
 /**  
 * Description : Custom VF page to add it on store page layout as an inline VF page.  
 *  
 * Created By : Abhi Tripathi  
 *  
 * Created Date : 12/10/2013  
 *  
 **/  
 -->  
 <apex:page controller="AccountHistoryController">  
   <apex:form >  
     <apex:pageBlock >  
       <table cellspacing="0" border="0" class="detailList list" cellpadding="0">  
         <thead class="rich-table-thead">  
           <tr class="headerRow">  
             <th class="headerRow" scope="col">Activity</th>  
             <th class="headerRow" scope="col">Date</th>  
             <th class="headerRow" scope="col">Change By</th>  
           </tr>  
         </thead>  
         <tbody>  
           <apex:repeat value="{!accountHistoriesWrapList}" var="accountHistoryWrap">  
             <tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);} " onmouseout="if (window.hiOff){hiOff(this);} " onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}">  
               <td colspan="3" style="text-align:center"><B><font size="3"><em><apex:outputField value="{!accountHistoryWrap.account.Name}"/></em></font></B></td>  
             </tr>  
             <apex:repeat value="{!accountHistoryWrap.accountHistories}" var="aHW">  
               <tr class="dataRow even first" onmouseover="if (window.hiOn){hiOn(this);} " onmouseout="if (window.hiOff){hiOff(this);} " onblur="if (window.hiOff){hiOff(this);}" onfocus="if (window.hiOn){hiOn(this);}">  
                 <td class="dataCell">  
                   Changed Grade from <b>"{!aHW.oldvalue}"</b> to <b>"{!aHW.newvalue}"</b>   
                 </td>  
                 <td class="dataCell">  
                   <apex:outputField value="{!aHW.createddate}"/>   
                 </td>  
                 <td class="dataCell">   
                   <apex:outputField value="{!aHW.createdby.Name}"/>   
                 </td>  
               </tr>   
             </apex:repeat>  
           </apex:repeat>  
         </tbody>  
       </table>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Test class

 @isTest(seeAllData=false)  
 private class Test_AccountHistoryController {  
   //Test method  
   static testMethod void myUnitTest() {  
     //List of Account  
     List<Account> accountList = new List<Account>();  
     //Insert Account  
     Account account = new Account(Name = 'test', Grade__c = 'A');  
     insert account;  
     //Insert another account  
     Account account2 = new Account(Name = 'ATest', Grade__c = 'C');  
     insert account2;  
     //Update Account  
     account.Grade__c = 'B';  
     accountList.add(account);  
     //Update Account  
     account2.Grade__c = 'D';  
     accountList.add(account2);  
     //Update account  
     update accountList;  
     //Account History  
     List<AccountHistory> accHistory = [Select Id From AccountHistory Where AccountId =: accountList];  
     //Test starts here  
     Test.startTest();  
     //Calling wrapper class  
     AccountHistoryWrapper wrapper = new AccountHistoryWrapper(account, accHistory);   
     //Controller  
     AccountHistoryController controller = new AccountHistoryController();  
     //Test stops here  
     Test.stopTest();  
     //Assert  
     System.assertEquals(true, Controller.accountHistoriesWrapList != null);  
   }  
 }  


Happy Coding
CHEERS........!!!!!!!

1 comment: