1

In my VF Page I have been asked to show a list of all the Notes and Attachments in one table, and sorted by the common LastModifiedDate ...

So I have a wrapper class in my custom controller:

public List<wNA> rowList {get; set;}  

public class wNA{

    public string Type {get; set;}
    public string Id {get; set;}
    public string fileName {get; set;}
    public string createdBy {get; set;}
    public datetime LastModifiedDate {get; set;}
    public string contentType {get; set;}
}

and in the constructor:

rowList = new List <wNA>();
wNA tr = new Wna();

system.debug('RFSCController CONSTRUCTOR get Attachments into List with Parent:' + recordid);

List<Attachment> lstAttachments= new List<Attachment>(); {
    lstAttachments = [SELECT Id, Name, LastModifiedDate, contentType, OwnerId from Attachment where ParentId =:recordid];
}

system.debug('RFSCController CONSTRUCTOR Attachments Count:' + lstAttachments.size());

if (lstAttachments.size()!=0){
    for (Integer iDx=0; iDx<lstAttachments.size(); iDx++){

        tr.Type = 'Attachment';
        tr.Id = lstAttachments[iDx].Id;
        tr.fileName = lstAttachments[iDx].Name;
        tr.createdBy = lstAttachments[iDx].OwnerId;
        tr.contentType = lstAttachments[iDx].contentType;
        tr.LastModifiedDate = lstAttachments[iDx].LastModifiedDate;

        rowList.add(tr);
    }       // Attachment att:[SELECT Id, Name, LastModifiedDate, contentType, OwnerId from Attachment where ParentId =:recordid]   
}

system.debug('RFSCController CONSTRUCTOR get Notes into List with Parent:' + recordid);

List<Note> lstNotes= new List<Note>(); {
    lstNotes = [SELECT Id, Title, ParentId, LastModifiedDate, createdbyid, body FROM Note where ParentId =:recordid];
}

if (lstNotes.size()!=0){
    for (Integer iDx=0; idx<lstNotes.size(); idx++){
        tr.Type = 'Note';
        tr.Id = lstNotes[iDx].Id;
        tr.fileName = lstNotes[iDx].Title;
        tr.createdBy = lstNotes[iDx].createdbyid;
        tr.LastModifiedDate = lstNotes[iDx].LastModifiedDate;           

        rowList.add(tr);       
    }           

}

in my previous question I was advised by crmprogdev to look at casting to a common object and comparable types

I can see in the docs here how to sort by a common field, but I cannot work out how to cast different objects to a common type...

I tried following the article on Custom Sort Order of Objects and came up with the below changes, but I am now stuck as really I need to cast the Notes and Attachments to a common type, then sort it...

public class wNA implements Comparable {

    public Note nt;
    public Attachment att;

    public wNA(Note myNote, Attachment myAtt){
        nt=myNote;
        att=myAtt;
    }

    public datetime compareToObj(Object compareTo){
        wNA compareTont = (wNA).compareTo;

        //datetime returnValue=Now();

        if (nt.LastModifiedDate>compareToObj.att.LastModifiedDate){ 
            return nt;}
        else if (nt.LastModifiedDate<>compareToObj.att.LastModifiedDate){
            return att;
        }

    }
    public string Type {get; set;}
    public string Id {get; set;}
    public string fileName {get; set;}
    public string createdBy {get; set;}
    public datetime LastModifiedDate {get; set;}
    public string contentType {get; set;}

    //Notes=[SELECT Id, Title, CreatedDate, createdbyid, body FROM Notes]

    //Attachments=[SELECT Id, Name, ContentType, OwnerId, CreatedDate  FROM Attachments];
}        

Can anyone help me get started?

What common type should I cast these attachments and Notes to?

And how do I then rebuild my list in order of the LastModifieddate field?

1

You need to implement the Comparable interface:

public class wNA implements Comparable {
    // The rest of this code remains the same; add the following function:
    public Integer compareTo(Object o) {
    return (LastModifiedDate.getTime()-((wNA)o).LastModifiedDate.getTime()).intValue();
    }
}

At this point, you can then sort your list after adding all the records to the common wrapper:

rowList.sort();

rowList = new List <wNA>();

List<Attachment> lstAttachments= [SELECT Id, Name, LastModifiedDate, contentType, OwnerId from Attachment where ParentId =:recordid];

    for (Integer iDx=0; iDx<lstAttachments.size(); iDx++){
        wNA tr = new wNA();
        tr.Type = 'Attachment';
        tr.Id = lstAttachments[iDx].Id;
        tr.fileName = lstAttachments[iDx].Name;
        tr.createdBy = lstAttachments[iDx].OwnerId;
        tr.contentType = lstAttachments[iDx].contentType;
        tr.LastModifiedDate = lstAttachments[iDx].LastModifiedDate;

        rowList.add(tr);
    }
}

List<Note> lstNotes= [SELECT Id, Title, ParentId, LastModifiedDate, createdbyid, body FROM Note where ParentId =:recordid];

    for (Integer iDx=0; idx<lstNotes.size(); idx++){
        wNA tr = new wNA();
        tr.Type = 'Note';
        tr.Id = lstNotes[iDx].Id;
        tr.fileName = lstNotes[iDx].Title;
        tr.createdBy = lstNotes[iDx].createdbyid;
        tr.LastModifiedDate = lstNotes[iDx].LastModifiedDate;           
        rowList.add(tr);
    }           
    rowList.sort();
}
  • 1
    @OurManInBananas You have some other errors in your code, which is probably why things are acting odd. You need to create a new wNA for each record you process. e.g. for (Integer iDx=0; iDx<lstAttachments.size(); iDx++){ tr = new wNA(); ... Most of the rest of your code is fine; you simply sort at the end of your constructor. – sfdcfox Jan 21 '16 at 18:16
  • 1
    @OurManInBananas I cleaned up your code with how it should work. You'll see that you were very close to the correct implementation; all you needed was a few minor tweaks. Also, compareToObj isn't how Comparable works. Just use the one I wrote here as a starting point. – sfdcfox Jan 21 '16 at 18:18
  • 1
    @OurManInBananas Comparable needs to be exactly public Integer compareTo(Object o). See my implementation for the correct use of Comparable. See how I convert the DateTime values to Long values (milliseconds since Jan 1, 1970), then subtract the two, and return the difference. Note that this version sorts from most recent to least recent. You can negate the result to change the order from least recent to most recent. – sfdcfox Jan 21 '16 at 18:24
  • 1
    @OurManInBananas change return (... to return -(.... As I just said, it's currently newest to oldest, so you'll want to reverse the order by negating the math. – sfdcfox Jan 21 '16 at 18:49
  • 1
    @sfdcfox please, check my answer regarding sorting salesforce.stackexchange.com/questions/106385/… – kurunve Jan 22 '16 at 14:37

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.