[SalesForce] ApexPages.currentPage().getParameters().get() returning NULL when testing


I have changed it as proposed by @mkorman 's post to:

Controller Class

public String myId {get; set;}
public PageReference controllerMethod(){
return NULL;

Visualforce Page

<apex:commandLink value="Button_Label" action="{!controllerMethod}" rendered="{!Object__c.BooleanField}"> 
    <apex:param name="param" value="{!Object__c.Id}" assignTo="{!myId}"/>

Test Class

VFController controller = New VFController();
controller.myId = myTestId;

But still getting NULL in System.Debug(myId);


In a method (let's call it controllerMethod) of a visualforce controller class (let's call it VFController) there's this line:

String myId = ApexPages.currentPage().getParameters().get('param');

that returns NULL when testing it.

That 'param' comes from the visualforce page (let's call it MyVFPage) that is controlled by that controller class:

<apex:commandLink value="Button_Label" action="{!controllerMethod}" rendered="{!Object__c.BooleanField}"> 
    <apex:param name="param" value="{!Object__c.Id}"/>

I have tried using

    PageReference VFPage = Page.MyVFPage;
    VFPage.getParameters().put('param', myTestId);
    VFControllercontroller = New VFController();

But still returning NULL.

Thanks in advance.

Best Answer

You should use ApexPages.currentPage() method for this approach:

ApexPages.currentPage().getParameters().put('param', Id);
Related Topic