0

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

  • 1
    See my answer below, but as a suggestion, instead of for (Integer iDx=0; iDx<lstAttachments.size(); iDx++), have you tried for (Attachment att : lstAttachments)? It's an easier-to-read syntax – mkorman Feb 11 '16 at 13:26
  • 1
    As an aside I don't think @testVisible does much. Your class is already public. – powlo Feb 11 '16 at 13:27
  • 1
    you have soql inside of for loops -- if your parent object has many notes and attachments, this will blow up on governor limits - better to query for users once and save result in a map – cropredy Feb 11 '16 at 14:42
  • @crop1645: But what if different attachments and notes are added by different users? How would I handle that, as we need to show for each note/attachment the creator? – Our Man in Bananas Feb 11 '16 at 14:53
1

You need to use the class name, not the instance name. You haven't posted your class name, so I assume it's RFSCController from the comments you've written in your code.

In that case, you initialise your variable by doing:

RFSCController.wNA wNA = new RFSCController.wNA();
|improve this answer|||||
  • my object is called Request_For_System_Change__c and my controller class is called RequestForSystemChangeController so I will try RequestForSystemChangeController.wNA wNA = new RequestForSystemChangeController.wNA(); – Our Man in Bananas Feb 11 '16 at 13:59

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.