function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amita TatarAmita Tatar 

not able to get current page id in test class, getting null in my class.

Hi all,

I am pasting below my apex class and test class. I am getting accID as null. Please help me where i am going wrong.
apex class----------------------

public class DisplayNotesNew{

public List<WrapperClass> listWrapper {get;set;}
public List<ContactWrapper> listWrapper1 {get;set;}
public List<OpportunityWrapper> listWrapper2 {get;set;}
public Id accId;
Public Account acc;
public List<Note> selectedNotes{get;set;}
public List<Contact> selectedContacts{get;set;}
public List<opportunity> selectedOpps{get;set;}
List<note> contactNote = new List<note>();
List<note> oppNote = new List<note>();
public ID noteID {get;set;}
Public String getRadioValue{get;set;}

public void assignNoteId() {
   Id note = ApexPages.currentPage().getParameters().get('note');
   system.debug('18-->'+note );
   if(note != null) {
      noteID = note;
   }
}    

public DisplayNotesNew(){
        
}
public DisplayNotesNew(ApexPages.StandardController Controller) {

accId = ApexPages.currentPage().getParameters().get('id');
system.debug('********accId************'+accId);
  
List<Note> listAcct = [SELECT Id,Title, Body,Parent.Id,CreatedDate FROM Note where Parent.Id =: accId order by ID DESC];
    system.debug('listAcct--------------------'+listAcct);
  if(listAcct.size() > 0) {  
      listWrapper = new List<WrapperClass>();      
      for(Note a : listAcct) {        
          listWrapper.add(new WrapperClass(a));
        }
    }
List<Contact> listContact = [Select Id, FirstName, LastName,name From Contact where AccountId =: accId order by name DESC];
    if(listContact.size() > 0){     
        listWrapper1 = new List<ContactWrapper>();          
        for(Contact c : listContact){            
            listWrapper1.add(new ContactWrapper(c));
        }
   }

List<Opportunity> listOpp = [select id,name,closeDate from Opportunity where AccountId =: accId order by closeDate ASC];
    if(listOpp.size() > 0){  
        listWrapper2 = new List<OpportunityWrapper>();             
        for(Opportunity o : listOpp){              
            listWrapper2.add(new OpportunityWrapper(o));
       }
   }
}
public pagereference assignNotes(){
system.debug('******noteID******'+noteID);
Note no = [select id,title,body,parentId from Note where id=:noteID limit 1];
system.debug('********no******'+no);

/*selectedNotes = new List<Note>();
system.debug('*********listWrapper ********'+listWrapper);
for(WrapperClass wrapAccountObj : listWrapper ) {
     if(wrapAccountObj.checkBool == true) {
        selectedNotes.add(wrapAccountObj.acct);
        system.debug('*********selectedNotes********'+selectedNotes);
     }  
}  */

selectedContacts = new List<Contact>();
 for(ContactWrapper wrapContactObj : listWrapper1){   
     if(wrapContactObj.checkBool == true){
         Note nt = new Note();  
         nt.Title = no.title;
         nt.Body = no.body;
         nt.ParentId = wrapContactObj.cons.Id; 
         contactNote.add(nt);
         //selectedContacts.add(wrapContactObj.cons);
         //system.debug('*********selectedContacts******'+selectedContacts);
       }
    }
insert contactNote;
    
selectedOpps = new List<Opportunity>();
  for(OpportunityWrapper wrapOppObj : listWrapper2){  
      if(wrapOppObj.checkBool == true){  
          Note nt = new Note(); 
          nt.Title = no.title;  
          nt.Body = no.Body;
          nt.parentId = wrapOppObj.ops.Id;
          oppNote.add(nt);
          //selectedOpps.add(wrapOppObj.ops);
         // system.debug('******selectedOpps*********'+selectedOpps);
     }
  }
insert oppNote;
  PageReference pg = new PageReference('/apex/DisplayAccountNotes?id='+accId);  /*Redirect on the same page*/  pg.setRedirect(true); return pg; 
}
public class WrapperClass{
   public Boolean checkBool {get;set;}
   public Note acct {get;set;}
   public WrapperClass(Note acct){
        this.acct = acct;
        checkBool = false;
       }        
    }
public class ContactWrapper{
   public Boolean checkBool {get;set;}
   public Contact cons {get;set;}
   public ContactWrapper(Contact cons){
        this.cons = cons;
        checkBool = false;
        }    
     }
public Class OpportunityWrapper{
   public Boolean checkBool {get;set;}
   public Opportunity ops {get;set;}
   public OpportunityWrapper(Opportunity ops){
        this.ops = ops;
        checkBool = false;
        }   
     }
}

Test Class-----------------------------------------

@isTest
public class DisplayNotesNewTestClass {

  static testmethod void test1(){
    
    Account acc = new Account(name='vivek112');
    insert acc;
   
    Note ne = new Note(parentid = acc.Id,body ='hi',title = 'Bye');
    insert ne;

    Contact con = new Contact(FirstName='vivek112', AccountId = acc.Id);
    List<Contact> lstcon = new List<Contact>();
    lstcon.add(con);   
        
    Opportunity opp = new opportunity(name = 'a',stageName = 'Closed Won',closedate = date.today(),Renewal_Date__c = date.today(),AccountId = acc.Id);
    List<Opportunity> lstopp = new List<Opportunity>();
    lstopp.add(opp);  
    system.debug('lstopp'+lstopp);
     
     Note note1 = new Note(parentid = acc.Id,title='test',body= 'hi');
    List<note> nList = new List<Note>();
    nList.add(note1);
   
    Note note2 = new Note(parentId = acc.Id,title = 'Hi',body = 'Hi');
    insert note2;
    system.debug('note2-------------'+note2);
    
  
    
    PageReference pageRef = Page.DisplayAccountNotes;
    Test.setCurrentPage(pageRef);      
    DisplayNotesNew obj = new DisplayNotesNew(new ApexPages.StandardController(acc));
    ApexPages.currentPage().getParameters().put('accId',acc.Id);
   // DisplayNotesNew controller = new DisplayNotesNew();
  	  Test.startTest();
 
    ApexPages.CurrentPage().getparameters().put('note',note2.Id);

    DisplayNotesNew objCustom = new DisplayNotesNew ();
    DisplayNotesNew.ContactWrapper objwrap = new DisplayNotesNew.ContactWrapper(con);
    DisplayNotesNew.OpportunityWrapper objwrap1 = new  DisplayNotesNew.OpportunityWrapper(opp); 
    DisplayNotesNew.WrapperClass objwrap2 = new DisplayNotesNew.WrapperClass(note1);
    Id accId = acc.Id;
    id note = note2.id;
    objCustom.assignNoteId();
    id noteID = note;
    objCustom.assignNotes();
    Test.stopTest();
    }
}

-Amita
Bhanu joshi 10Bhanu joshi 10
replace 
    ApexPages.currentPage().getParameters().put('accId',acc.Id);
with

    ApexPages.currentPage().getParameters().put('id',acc.Id);  in test class.
Hope this works
Amita TatarAmita Tatar
Hi Bhanu,

I got the accid. But now stuck at line no 73.. where i have the checkbool condition. How do i add it in test class?
Asif Ali MAsif Ali M
You are not getting the coverage for assignNotes method because your wrapper classes have a default value FALSE on checkBool property. Add these 2 lines after you created ContactWrapper ad OpprtunityWrapper object.
objwrap.checkBool = true;
objwrap1.checkBool = true;

 
Amita TatarAmita Tatar
Hi Asif,

okay i will do that and let you know if it works
Amita TatarAmita Tatar
Hi Asif,
I incuded those two lines, still test is getting failed and giving me null pointer exception
Asif Ali MAsif Ali M
Null pointer exception will generally occur when you are trying to access a property on an object when it is null. 
Can you please share the full error message?
Bhanu joshi 10Bhanu joshi 10

Hi 

The boolean is  false because when you are adding a note in wrapper class it make it always false.(see you line 39 and 104.
Pass two paremeter one for object and other for Boolean.
Hope this works !!