+ Start a Discussion
Eager-2-LearnEager-2-Learn 

How do I sort a Helper Class

Hi,

 

I have the following helper class and I am instantiating it as such...  How can I sort on the Account Id?  I need to iterate through the helper clas records in order by account so that I can sum up any related opportunties.  It seems that SF has a limitation on an Aggregate query where I cannot include the account id since it is two paraents up from my Opportunity related object.  I tried all kinds of approaches so I was thinking that this helper class would allow me to iterate through and code for the possibility of multiple opps.

 

...

List<Enrollment_QueryBase.EnrollmentContractSummaryHelperClass> EnrollmentSummary {get; set;}

...

EnrollmentSummary = new List<Enrollment_QueryBase.EnrollmentContractSummaryHelperClass>();

 

for (....

 

    EnrollmentSummary.add(new Enrollment_QueryBase.EnrollmentContractSummaryHelperClass(q, AccountIdMap));

}

 

EnrollmentSummary.sort();  **** this line fails  -- I thought it would sort by the first field in my class!

...

...

 

I appreciate any help you can provide.

 

 

 

     public class EnrollmentContractSummaryHelperClass {
         {
             // Default just to prevent any possible null reference errors
             // really not needed if class is called correctly
             this.RptDt = '190001';            
         }
         public Id AcctId {get; set;}
         public Id OppId {get; set;}         
         public String RptDt {get; set;}
         public Double ContractSum {get; set;}

         public EnrollmentContractSummaryHelperClass ( AggregateResult q, Map<id, Id> AccountIds ) {
             id OppId = (Id)q.get('Opportunity__c');
             this.AcctId = AccountIds.get(OppId);
             this.OppId = OppId; 
             this.RptDt = (String)q.get('Rpt_Dt__c');                           
             this.ContractSum = (Double)q.get('expr0');          
         }        
         
     }      

 

Best Answer chosen by Admin (Salesforce Developers) 
k_bentsenk_bentsen

Modify your class declaration line as such:

 

 public class EnrollmentContractSummaryHelperClass implements Comparable{

 

And add the following method:

 

global Integer compareTo(Object compareToEnrollCont) {
  EnrollmentContractSummaryHelperClass compareEnrollCont = (EnrollmentContractSummaryHelperClass)compareToEnrollCont;
  if (AcctId == compareEnrollCont.AcctId) return 0;
  if (AcctId > compareEnrollCont.AcctId) return 1;
  return -1; 
}

 

All Answers

k_bentsenk_bentsen

Modify your class declaration line as such:

 

 public class EnrollmentContractSummaryHelperClass implements Comparable{

 

And add the following method:

 

global Integer compareTo(Object compareToEnrollCont) {
  EnrollmentContractSummaryHelperClass compareEnrollCont = (EnrollmentContractSummaryHelperClass)compareToEnrollCont;
  if (AcctId == compareEnrollCont.AcctId) return 0;
  if (AcctId > compareEnrollCont.AcctId) return 1;
  return -1; 
}

 

This was selected as the best answer
Eager-2-LearnEager-2-Learn

Hi k_bentsen,

 

I may be doing something wrong or some additional code is required because I am getting the following error;

 Compile Error: Variable does not exist: compareToEnrollCont at line 283 column 109

 

In addition do I simply now use the sort method and this all works seemlessly or is there a specific way I must call the compareToEnrollCont method?

Eager-2-LearnEager-2-Learn

I figured it out the corrections are in red.  Thank you for getting me pasted this sort issue. Now I just need to call it and see if it all works but at least it compiles now.

 

global Integer compareTo(Object compareToEnrollCont) {
  EnrollmentContractSummaryHelperClass compareEnrollHelperClass = (EnrollmentContractSummaryHelperClass)compareToEnrollCont;
  if (AcctId == compareEnrollHelperClass.AcctId) return 0;
  if (AcctId > compareEnrollHelperClass.AcctId) return 1;
  return -1;
}