5

Update:

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

Controller Class

[...]
public String myId {get; set;}
[...]
public PageReference controllerMethod(){
    [...]
    System.Debug(myId);
    [...]
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}"/>
</apex:commandLink>
[...]

Test Class

[...]
VFController controller = New VFController();
[...]
controller.myId = myTestId;
controller.controllerMethod();
[...]

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

Original:

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}"/>
</apex:commandLink>

I have tried using

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

But still returning NULL.

Thanks in advance.

  • Test.setCurrentPageReference is completely useless. Don't bother. – Adrian Larson Dec 16 '15 at 17:19
6

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

ApexPages.currentPage().getParameters().put('param', Id);
  • I have changed it to PageReference VFPage = Page.MyVFPage; Test.setCurrentPage(VFPage); ApexPages.currentPage().getParameters().put('param', Id); VFControllercontroller controller = New VFController(); and still not working. – Iliablun Dec 16 '15 at 14:42
  • 1
    @Iliablun Try to rename your variable from Id to something else, for example to myId. I think that Id is reserved name. – Sergej Utko Dec 16 '15 at 14:49
  • 1
    One shouldn't need to use ApexPages.currentPage()... to set parameters, anyPgRef.getParameters().put(someparamName,someparamValue) works fine (as long as test.setCurrentPageReference(anyPgRef) was called beforehand. – cropredy Dec 16 '15 at 17:05
  • @mast0r, the names of variables, methods, etc. are just examlpes. By the way, I have changed the code as proposed by mkorman, check the first message. Thanks to you all. – Iliablun Dec 16 '15 at 17:37
3

If you want to pass an argument to a method in the same controller, you don't need to parse the query string. You can just assign the value to a public property via the "apex:param" and read it in your method.

Take a look at this:

http://blog.jeffdouglas.com/2010/03/03/passing-parameters-with-a-commandlink/

In your case, in VFController you could:

  • declare a public property called "param"
  • in controllerMethod refernce param directly
  • I have edited the opening message after trying this. I still couldn't fix it. Thanks. – Iliablun Dec 16 '15 at 17:30

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.