1

I am trying to sort list x in descending order by Delta__c. I have been told that I should use a wrapper class and use the implements comparable class. I am getting the error even though Delta__c is an integer field:

Return value must be of type: Integer for the compareTO class

global Class Engagement {

  public List<Engagement__c> top10 {get;set;}   

  public Engagement__c[] getDelta(){

   List<Engagement__c> x = [SELECT Name, ID,Engagement_Score__c,CreatedDate,Account_ID__c,Delta__c,Account_Name__c,Strategic_Account_Tier__c 
                      FROM Engagement__c 
                      WHERE (CreatedDate2EqualsToday_1__c = TRUE OR CreatedDate2MinusYesterday__c = 0 OR CreatedDate2MinusYesterday2__c=2) AND Account_Sales_Team__c = 'US'
                      ORDER BY Account_ID__c,CreatedDate ASC];

    if(!x.isEmpty())
   {
   for(Integer i=1; i<x.size();i++)
    {
        if(x[i].Account_ID__c == x[i-1].Account_ID__c)
        {
             x[i].Delta__c = x[i].Engagement_Score__c-x[i-1].Engagement_Score__c;              
             x.remove(i-1);                  
          i++;            
        }     
        else system.debug('nothing found');
    }
    }
    EngagementWrapper[] temp = new EngagementWrapper[0];
    for(Engagement__c record: x) {
        temp.add(new EngagementWrapper(record));
    }
    temp.sort();
    Engagement__c[] y = new Engagement__c[0];
    for(EngagementWrapper wrapper: temp) {
        y.add(wrapper.record);
    }
    return y;
  }

  global class EngagementWrapper implements Comparable {
    Engagement__c record;
    EngagementWrapper(Engagement__c engagement) {
       record = engagement;
}
global Integer compareTo(Object other) {
    return ((EngagementWrapper)other).record.Delta__c-record.Delta__c;
}
 }

 } 

1 Answer 1

3

As the error says, you need to return an Integer. There is no such thing as an Integer field in Salesforce. All Number fields return a Double even if they have 0 trailing digits.

Also, use public, not global. Never use global unless you know for darn sure why.

public Integer compareTo(Object instance)
{
    MyWrapper that = (MyWrapper)instance;

    // below gives you nulls last
    // reverse the sign to get nulls first
    if (that.record.Delta__c == null) return 1;
    if (this.record.Delta__c == null) return -1;        

    // you can ignore the rounding if you have 0 trailing digits
    return (Integer)(that.record.Delta__c - this.record.Delta__c).round(RoundingMode.UP);
}

Note the use of ROUNDINGMODE.UP (away from zero). This rounding mechanism ensures you don't accidentally sorting two records in the wrong order because their values are within 1 of each other.

5
  • Thanks. That works. Though now I get the error 'Argument cannot be Null' . I guess values between wrapper and getdelta are not passing.
    – Mike
    Aug 30, 2016 at 19:09
  • @Mike I added some null checking logic to my example.
    – Adrian Larson
    Aug 30, 2016 at 19:19
  • Perfect! Thank you for the explanation. In this, is it also possible to remove nulls from list?
    – Mike
    Aug 30, 2016 at 19:28
  • @Mike You should filter them out in your initial for loop. for (Engagement__c record : x) { if (record.Delta__c != null) { temp.add(new MyWrapper(record)); } }.
    – Adrian Larson
    Aug 30, 2016 at 19:29
  • Oh how did I not see that! :-| Thank you so much!!
    – Mike
    Aug 30, 2016 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.