I looked at these existing questions but they didn't answer my specific case:
in my controller I have the below code which stores all the Notes and Attachments on my requests object:
@TestVisible public List<wNA> rowList {get; set;}
@TestVisible public class wNA implements Comparable {
// see
// https://salesforce.stackexchange.com/questions/106275/how-to-sort-a-custom-list-of-notes-and-attachments-by-lastmodifieddate
// and
// https://salesforce.stackexchange.com/questions/106385/notes-attachments-wrapper-class-not-sorting-by-lastmodifieddate-correctly?noredirect=1#comment145451_106385
// calling the Sort method on a List type derived from this class calls the below comparison for each member of the list
public Integer compareTo(Object o) {
system.debug('Sorting difference: ' + (LastModifiedDate.getTime()-((wNA)o).LastModifiedDate.getTime()).intValue());
return -(LastModifiedDate.getTime()-((wNA)o).LastModifiedDate.getTime()).intValue();
}
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;}
public string LastModifiedDtStr {get; set;}
}
In the constructor I call the below function to retrieve all Notes and attachments and sort them like this:
public void getNotes_and_Attachments(){
system.debug('RFSCController Constructor GET ATTACHMENTS');
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();
String strLastModified = lstAttachments[iDx].LastModifiedDate.format('EEE, d MMM yyyy HH:mm');
System.debug(' @@@@ Attachments strLastModified: '+strLastModified);
tr.Type = 'Attachment';
tr.Id = lstAttachments[iDx].Id;
tr.fileName = lstAttachments[iDx].Name;
tr.createdBy = [SELECT Id, Name from User where Id =:lstAttachments[iDx].OwnerId].Name;
tr.contentType = lstAttachments[iDx].contentType;
tr.LastModifiedDate = lstAttachments[iDx].LastModifiedDate;
tr.LastModifiedDtStr = strLastModified;
rowList.add(tr);
}
system.debug('RFSCController Constructor GET NOTES');
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();
String strLastModified = lstNotes[iDx].LastModifiedDate.format('EEE, d MMM yyyy HH:mm');
System.debug(' @@@@ strLastModified: '+strLastModified);
// MAILSHOT New, Save, Edit, Save, Submit, Approve Test
tr.Type = 'Note';
tr.Id = lstNotes[iDx].Id;
tr.fileName = lstNotes[iDx].Title;
tr.createdBy = [SELECT Id, Name from User where Id =:lstNotes[iDx].createdbyid].Name;
tr.LastModifiedDate = lstNotes[iDx].LastModifiedDate;
tr.LastModifiedDtStr = strLastModified;
rowList.add(tr);
}
rowList.sort();
}
Now in my test class I want to test that wrapper calss code.
I tried this but I cannot get it to compile:
controller.wNA wNA = new controller.wNA();
and I also tried: //rowList=controller.getNotes_and_Attachments();
the compile error I am getting is:
Invalid type: controller.wNA
for (Integer iDx=0; iDx<lstAttachments.size(); iDx++)
, have you triedfor (Attachment att : lstAttachments)
? It's an easier-to-read syntax