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

How to call setParameters from test method?

I need to set a query parameter in a test method.  The documentation makes only a quick reference to this, saying:

To set a query string parameter:

  • If you're writing a custom controller, use the setParameters() method with ApexPages.currentPage() to add a query parameter in a test method. For example:
    String key = 'name';
    String value = 'Caroline';
    ApexPages.currentPage().setParameters().put(key, value);
     The setParameters() method is only valid inside test methods.

However, when I put this in my test method, I get the error:


Save error: Method does not exist or incorrect signature: [System.PageReference].setParameters()    Testm62ConsoleController.cls    /m62 Sandbox/src/classes    line 80 save problem


Here is my test method:


 static testMethod void testConsoleController() {

		// Find a telesales user for the tests
        Profile p = [select id from profile where name='Telesales']; 
        User u = new User(alias = 'standt', email='', 
            emailencodingkey='UTF-8', lastname='Testing', profileid = p.Id, 

        System.runAs(u) {
		//Set up the data
		List<Account> accs = new List<Account>();
		for (Integer i=0; i<50; i++) {
			Account a = new Account(
				Name = 'Test Account 1',
				BillingCountry = 'United Kingdom',
				Phone = '+44 (0)12679 121 5555',
				To_be_cleansed__c = true
		insert accs;
		List<Contact> conts = new List<Contact>();
		List<Task> tasks = new List<Task>();
		// Create two contacts for each account
		for (Integer i=0; i<50; i++) {
			Contact c = new Contact();
			c.FirstName = 'Test';
			c.LastName = 'Contact 1';
			c.AccountId = accs[i].Id;
			c.MailingCountry = 'United Kingdom';
			c.Email = '';
			c.Phone = '+44 (0)1423 564888';
			Contact c1 = new Contact();
			c1.FirstName = 'Test';
			c1.LastName = 'Contact 1';
			c1.AccountId = accs[i].Id;
			c1.MailingCountry = 'United Kingdom';
			c1.Email = '';
			c1.Phone = '+32 (0) 2 679 12 11';
			// Add a task for each account
			Task t = new Task(WhatId=accs[i].Id, Type='Telephone call', Subject='Test subject', Status='Not started', 
		insert conts;
		insert tasks;
        PageReference pageRef = Page.newConsole;  // Instantiate telesales console page
        //Instantiate and construct the controller class.   
        m62ConsoleController controller = new m62ConsoleController();		
		controller.selectedAcc = accs[4];
		if (controller.getHasPreviousAcc()) controller.previousAcc();
		if (controller.getHasNextAcc()) controller.nextAcc();
		String ids = accs[6].Id;
		ApexPages.currentPage().setParameters().put('AccSel', ids);

		ids = conts[12].Id;
		ApexPages.currentPage().setParameters().put('ConSel', ids);


 Can anyone advise what I'm doing wrong? I've searched all over for more information but can't find any.



Best Answer chosen by Admin (Salesforce Developers) 

it is,


ApexPages.currentPage().getParameters().put('key', 'value'); //it is getParameters not setParameters

All Answers


it is,


ApexPages.currentPage().getParameters().put('key', 'value'); //it is getParameters not setParameters

This was selected as the best answer

Thank you - it is obviously an error in the documentation (Salesforce please note) that caused me wasted hours.  My test method is working perfectly now and I've got the coverage needed.


Thanks again.


Hi m62Dev,


Thanks for bringing the code error to our attention--it'll be fixed shortly.


In the future, please indicate which books you're receiving code samples from. Additionally, there's no setParameters() method defined in either the Apex or Visualforce guides, so the problem is only contained in that single Cookbook recipe.


I tend to search Developer force rather than read the manuals when I'm looking for an answer to a specific problem.   I searched using the keywords "setting query string test method" and it was the first item in the results.  I read a number of Wiki pages on writing unit tests and they didn't address this particular issue.  So when I implemented the one reference I could find, I was puzzled when it didn't work and when I couldn't find any documentation on it - should have indicated to me that the cookbook was wrong, but I usually assume it is my mistake and not salesforce's!


I recommend searching the Visualforce Developer's Guide and Apex Devloper's Guide directly, that usually lets me find what I am looking for