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
Dave BerenatoDave Berenato 

getParameters is showing as null in Test Class

I have an Apex Class that pulls a UserID using getParameters. It works in the Visualforce page but in the Test Class it shows as "System.QueryException: invalid ID field: null"
 
public with sharing class TelemarketerDialing {
    
    public TelemarketerDialing(){
        if(ApexPages.currentpage().getParameters().get('leadgenerator') == null){
            myParam = UserInfo.getUserId();
        }
        else{
            myParam = ApexPages.currentpage().getParameters().get('leadgenerator');
        }
        }
    
    
      public String myParam {get; set;}
        
	  public String sortOrder = ' Last_Call__c ';
      public String ascendingOrDescending = ' ASC ';
    
      public String myRequest = ' SELECT id, Name, FirstName, LastName, Last_Call__c,Phone,Email,MobilePhone,Second_Property_Owner__c,Age__c,Equity_Dollar__c,'+
                      '	Actual_Sale_Date__c, Foreclosure_Status__c, Total_Calls_Live__c, Distance_from_FAIR_Program__c, Number_of_Phone_Numbers__c,Equity_Percentage__c,'+
            		  '	Property_Address__c,Property_City__c,Property_State__c,Property_Zip_Code__c,Verified_Lender__c,Prospecting_Criteria__c,'+
            	      '	Language_Spoken_at_Home__c,Total_Talk_Time_minutes__c,Combined_LTV_Ratio__c,'+
            		  ' Account.Id,Account.View_Note_URL__c,Account.New_Note_URL__c '+
                  	  ' FROM Contact '+
                      ' WHERE Contact_Stage__c = \'Dialing\' AND Prospecting_Criteria__c = TRUE AND OwnerId = \'' + myParam + '\' ORDER BY ';

List<Contact> contacts;
       public List<Contact> getContacts() {
   		contacts = new List<Contact>();
        contacts = Database.query(myRequest + sortOrder + ascendingOrDescending);
        return contacts;}

      public void saveAndReturn() {
        update contacts;
        }
}

With the following Test Class that I confirmed can Pass if I delete the getParameters section and replace the "myParam" in "myRequest" with "UserInfo.getUserId()".
 
@isTest
public class TelemarketerDialingTest {

    static testMethod void TestDialing(){
         
    Account testaccount = new Account();
    testaccount.name = '123 Test Drive';
    testaccount.RecordTypeId = '0126A0000004Qo4';
    testaccount.APN__c = '123';
    testaccount.Foreclosure_Status__c = 'Auction';
    testaccount.Property_Zip_Code__c = '90001';
    insert testaccount;
    system.debug(testaccount.id);
         
    Contact testcontact = new Contact();
    testcontact.Contact_Stage__c = 'Dialing';
    testcontact.Phone_1__c = '6106754043';
    testcontact.Accountid = testaccount.id;
    testcontact.LastName = 'Dave';
    testcontact.OwnerId = UserInfo.getUserId();
    testcontact.RecordTypeId = '0126A0000004QlU';
    insert testcontact;
    
    TelemarketerDialing td = new TelemarketerDialing();
    List<Contact> list1 = td.getContacts();
    td.saveAndReturn();
    system.assertEquals(1, list1.size());
    }
    
}

 
Best Answer chosen by Dave Berenato
Alain CabonAlain Cabon
Hi Dave,

dev_arya was right but as usual with your question, there is often a "surprise" (and that could be a good question in a quizz).

Probably already solved nevertheless here is a solution (among many other ways).

When myRequest is evaluated, myParam is null.  
When the value of myParam is initialized into the constructor, myRequest is not re-evaluated so myParam is still null into myRequest until its use by the method getContacts.
 
public String myRequest  {get; set;}
 
public with sharing class TelemarketerDialing {
    
    public TelemarketerDialing(){
        if(ApexPages.currentpage().getParameters().get('leadgenerator') == null){
            myParam = UserInfo.getUserId();
        }
        else{
            myParam = ApexPages.currentpage().getParameters().get('leadgenerator');
        }
        myRequest = ' SELECT id, Name, FirstName, LastName, Last_Call__c,Phone,Email,MobilePhone,Second_Property_Owner__c,Age__c,Equity_Dollar__c,'+ ' Actual_Sale_Date__c, Foreclosure_Status__c, Total_Calls_Live__c, Distance_from_FAIR_Program__c, Number_of_Phone_Numbers__c,Equity_Percentage__c,'+ ' Property_Address__c,Property_City__c,Property_State__c,Property_Zip_Code__c,Verified_Lender__c,Prospecting_Criteria__c,'+ ' Language_Spoken_at_Home__c,Total_Talk_Time_minutes__c,Combined_LTV_Ratio__c,'+ ' Account.Id,Account.View_Note_URL__c,Account.New_Note_URL__c '+ ' FROM Contact '+ ' WHERE Contact_Stage__c = \'Dialing\' AND Prospecting_Criteria__c = TRUE AND OwnerId = \'' + myParam + '\' ORDER BY ';
   
   }
   
   public String myParam {get; set;} 
   public String myRequest  {get; set;}    
   public String sortOrder = ' Last_Call__c ';
   public String ascendingOrDescending = ' ASC ';
    

Alain

All Answers

Dev_AryaDev_Arya
Hi Dave,

The test class in Apex does not have a context to the current page hence  getCurrent().getParameter() does not work. You need to explicitly set the page and  provide the parameter to ur test class before you call the controller method to be tested:
ApexPages.currentPage().getParameters().put('leadgenerator',somevalue_for_leadgenerator);
TelemarketerDialing td = new TelemarketerDialing();
Hope this helps.
Cheers,
Dev
Dev_AryaDev_Arya
Hi Dave, if you got the answer, could you please mark the question as solved. Thanks.
Dave BerenatoDave Berenato
Hi dev_arya,

I still got the same error: System.QueryException: invalid ID field: null
 
@isTest
public class TelemarketerDialingTest {

    static testMethod void TestDialing(){
         
    Account testaccount = new Account();
    testaccount.name = '123 Test Drive';
    testaccount.RecordTypeId = '0126A0000004Qo4';
    testaccount.APN__c = '123';
    testaccount.Foreclosure_Status__c = 'Auction';
    testaccount.Property_Zip_Code__c = '90001';
    insert testaccount;
    system.debug(testaccount.id);
         
    Contact testcontact = new Contact();
    testcontact.Contact_Stage__c = 'Dialing';
    testcontact.Phone_1__c = '6106754043';
    testcontact.Accountid = testaccount.id;
    testcontact.LastName = 'Dave';
    testcontact.OwnerId = UserInfo.getUserId();
    testcontact.RecordTypeId = '0126A0000004QlU';
    insert testcontact;
        
	ApexPages.currentPage().getParameters().put('leadgenerator',testcontact.OwnerID);
    TelemarketerDialing td = new TelemarketerDialing();
    List<Contact> list1 = td.getContacts();
    td.saveAndReturn();
    system.assertEquals(1, list1.size());
    }
}

 
Alain CabonAlain Cabon
Hi Dave,

dev_arya was right but as usual with your question, there is often a "surprise" (and that could be a good question in a quizz).

Probably already solved nevertheless here is a solution (among many other ways).

When myRequest is evaluated, myParam is null.  
When the value of myParam is initialized into the constructor, myRequest is not re-evaluated so myParam is still null into myRequest until its use by the method getContacts.
 
public String myRequest  {get; set;}
 
public with sharing class TelemarketerDialing {
    
    public TelemarketerDialing(){
        if(ApexPages.currentpage().getParameters().get('leadgenerator') == null){
            myParam = UserInfo.getUserId();
        }
        else{
            myParam = ApexPages.currentpage().getParameters().get('leadgenerator');
        }
        myRequest = ' SELECT id, Name, FirstName, LastName, Last_Call__c,Phone,Email,MobilePhone,Second_Property_Owner__c,Age__c,Equity_Dollar__c,'+ ' Actual_Sale_Date__c, Foreclosure_Status__c, Total_Calls_Live__c, Distance_from_FAIR_Program__c, Number_of_Phone_Numbers__c,Equity_Percentage__c,'+ ' Property_Address__c,Property_City__c,Property_State__c,Property_Zip_Code__c,Verified_Lender__c,Prospecting_Criteria__c,'+ ' Language_Spoken_at_Home__c,Total_Talk_Time_minutes__c,Combined_LTV_Ratio__c,'+ ' Account.Id,Account.View_Note_URL__c,Account.New_Note_URL__c '+ ' FROM Contact '+ ' WHERE Contact_Stage__c = \'Dialing\' AND Prospecting_Criteria__c = TRUE AND OwnerId = \'' + myParam + '\' ORDER BY ';
   
   }
   
   public String myParam {get; set;} 
   public String myRequest  {get; set;}    
   public String sortOrder = ' Last_Call__c ';
   public String ascendingOrDescending = ' ASC ';
    

Alain
This was selected as the best answer
Alain CabonAlain Cabon
or this given that myParam could change:

public String myRequest  {get; set;}  ​   // myRequest  never used outside getContacts() nor on the screen (sometimes the query is shown for debugging)
public List<Contact> getContacts() {
        String myRequest = ' SELECT id, Name, FirstName, LastName, Last_Call__c,Phone,Email,MobilePhone,Second_Property_Owner__c,Age__c,Equity_Dollar__c,'+ ' Actual_Sale_Date__c, Foreclosure_Status__c, Total_Calls_Live__c, Distance_from_FAIR_Program__c, Number_of_Phone_Numbers__c,Equity_Percentage__c,'+ ' Property_Address__c,Property_City__c,Property_State__c,Property_Zip_Code__c,Verified_Lender__c,Prospecting_Criteria__c,'+ ' Language_Spoken_at_Home__c,Total_Talk_Time_minutes__c,Combined_LTV_Ratio__c,'+ ' Account.Id,Account.View_Note_URL__c,Account.New_Note_URL__c '+ ' FROM Contact '+ ' WHERE Contact_Stage__c = \'Dialing\' AND Prospecting_Criteria__c = TRUE AND OwnerId = \'' + myParam + '\' ORDER BY '; 
   		contacts = new List<Contact>();
        contacts = Database.query(myRequest + sortOrder + ascendingOrDescending);
        return contacts;
}
There are many alternatives.

Alain
Dave BerenatoDave Berenato
This worked absolutely perflectly. Alain, you have helped me with so many apex questions that I had. I would love to learn more from you, do you have a Github or a way I can get in touch with you?
Alain CabonAlain Cabon
Hello Dave;

I will have my own projects in github at the end of the year I hope but right now, I am just very pleased that I could help some grateful people like you here (even a little) (... and push up the counter up to 5,000 points perhaps (a very difficult challenge)).

By the way, your questions are often surprising because even if your code is short, there are little traps that we don't see sometimes at first glance. 

Have a nice evening.
Alain