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?