Direkt zum Hauptbereich

Sortieren einer APEX Liste mit Wrapper Objekten einfach gemacht

Eine Visualforce Seite zeigt alle Feiertage in diesem Jahr.
Jeder Feiertag entspricht einem Salesforce Objekt.
Die Liste wird dynamisch aufgebaut und ist somit unsortiert.


Der entsprechende Code sieht ungefähr so aus:

public list<HOLIDAYS> getHoliday(){
  list<HOLIDAYS> lstHolidays = new list<HOLIDAYS>();
  ...
  lstHolidays.add(new HOLIDAYS(obj, true));
  ...
  return lstHolidays;
}
/******************************
*** HOLIDAYS
******************************
 public class HOLIDAYS{
  public Holiday__c objHoliday{get; set;}
  public String holidayName{get; set;}
  public Id holidayId{get; set;}
  public String holidayDate{get; set;}
  public Decimal duration{get; set;}
  public Boolean isChecked{get; set;}


  public HOLIDAYS(Holiday__c h, Boolean isChecked){
   this.objHoliday = h;
   this.holidayName = h.Name;
   this.holidayId = h.id;

   this.holidayDate = h.Date__c.format();
   this.duration = h.Duration__c;
   this.isChecked = isChecked;
  }
 }

Mit dem Einsatz der sort() Methode und einer kleinen Anpassung der Wrapper Klasse wird die Liste nach Datum sortiert ausgegeben:

public list<HOLIDAYS> getHoliday(){
  list<HOLIDAYS> lstHolidays = new list<HOLIDAYS>();
  ...
  lstHolidays.add(new HOLIDAYS(obj, true));
  ...
  lstHolidays.sort();
  return lstHolidays;
}

Einsatz von Comparable Interface

public class HOLIDAYS implements Comparable{
public Holiday__c objHoliday{get; set;}
public String holidayName{get; set;}
public Id holidayId{get; set;}
public String holidayDate{get; set;}
public Decimal duration{get; set;}
public Boolean isChecked{get; set;}

public HOLIDAYS(Holiday__c h, Boolean isChecked){
  this.objHoliday = h;
  this.holidayName = h.Name;
  this.holidayId = h.id;
  this.holidayDate = h.Date__c.format();
  this.duration = h.Duration__c;
  this.isChecked = isChecked;
}


  // Compare holidays based on the date
  public Integer compareTo(Object compareTo) {
    HOLIDAYS compareToHoliday = (HOLIDAYS)compareTo;
    // The return value of 0 indicates that both elements are equal.
    Integer returnValue = 0;
    if (objHoliday.Date__c > compareToHoliday.objHoliday.Date__c) {
      // Set return value to a positive value.
      returnValue = 1;
    else if (objHoliday.Date__c < compareToHoliday.objHoliday.Date__c) {
      // Set return value to a negative value.
      returnValue = -1;
    }
    return returnValue;
  }
}

Kommentare

Beliebte Posts aus diesem Blog

Community Builder funktioniert nicht

Nachdem ich eine in einer Sandbox konfigurierte und getestete Lightning Community per Changeset auf die Produktion übertrage und bereitgestellt hatte, stellte ich fest, dass sich die Community nicht konfigurieren lässt. Das Deployment lief fehlerfrei durch. Alle Komponenten der Lightning Community sind verfügbar. Jedoch erscheint die Fehlermeldung Cannot read property 'def' of undefined sobald ich auf den "Builder" - Link klicke. Folgendes Workaround löst das Problem: 1. Go to "All Communities" and click on "Workspaces" beside the problematic community. 2. Go to "Administration | Pages" and click on "Go to Site.com Studio". 3. Once site.com studio has finished loading, click on the "Site Actions" icon (small cog in top right of screen), and select "Export This Site". 4. When prompted, specify a local location to save the site export, and wait for the file download to complete. 5. After the d...

Bad value for restricted picklist field

Der Einsatz von "Restricted Picklists" bereitet spätestens im Deployment Kopfschmerzen. Basiert das Deployment auf Basis eines Drittanbietertools, dann sind die Kopfschmerzen noch intensiver. In meinem Fall habe ich versucht, ein neues Picklist-Feld mit Copado zu deployen. Während der Bereitstellung bekomme ich die folgende Fehlermeldung: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, bad value for restricted picklist field: Z012: [CountryGroup__c] Das neue Picklist-Feld übernimmt alle Werte aus einem Global Value Set. Das bedeutet, die Option "Restrict to the values defined in the value set" ist automatisch aktiv und lässt sich nicht deaktivieren. Eine APEX-Testklasse beschreibt ebenfalls die neue Pickliste. Mit dem folgenden Workaround konnte ich das Deployment-Problem lösen: 1) Global Value Set samt Pickliste per Changeset in die Zielorg übertragen und bereitstellen ggf. Prof...

Crazy SOQL

Genauso habe ich heute geschaut, als ich den folgenden Code ausgeführt und das Ergebnis ausgewertet habe: CustomObj__c obj = [select LookupField__c from CustomObj__c where LookupField__c != NULL AND Id = 'hereisavalidid']; system.debug(' LookupField__c darf nicht NULL sein '); if(obj.LookupField__c == null){     system.debug(' Also doch NULL '); } Und was sehen meine müde Augen im Log... LookupField__c ist ein Lookup- und Pflichtfeld, somit darf eigentlich per Definition nicht NULL sein. Offensichtlich gibt es (alte) Daten im System mit dem  LookupField__c = NULL Habe erwartet, dass die SOQL Abfrage die NULL-Daten filtert.