-1

I have the following class:

public class ABC{
    public ABC(Apexpages.standardcontroller s){
        string param1 = ApexPages.currentPage().getParameters().get('v1');
        //blah blah blah
    }
    public void method1(){
    }
}

I need to call method1 but at the same time pass parameters. I tried as we do in test classes:

ApexPages.currentPage().getParameters().put('v1','somevalue');
ABC cont = new ABC();

But this seems to not be working

4
  • ABC cont = new ABC(); cont.method1() ; this works else move the param1 as class variable
    – bharath
    Commented Feb 9, 2017 at 7:30
  • But how are we setting the value of param1. Anyways can't move it anywhere
    – starhunter
    Commented Feb 9, 2017 at 7:36
  • Set param1 to be an instance variable of the ABC class. In the constructor you can populate it with the GET parameter, but in anonymous code you can just set it to a value. Commented Feb 9, 2017 at 7:45
  • Hmm so that would mean I have to make param1 a static variable and set the value from execute anonymous.
    – starhunter
    Commented Feb 9, 2017 at 8:00

2 Answers 2

1

You can't reference ApexPages.currentPage() in a non-VF context (i.e. execute anonymous).

You should either test using a browser or use a test method

PageReference pg = Test.setCurrentpage(somePage);
pg.getParameters().put('v1','someval');
ABC cont = new ABC(new Apexpages.standardcontroller(someSobject));
0

Your class should be modified as shown below:

public class ABC{
    public string param1;
    public ABC(Apexpages.standardcontroller s){
        if(param1 != null){
            param1 = ApexPages.currentPage().getParameters().get('v1');
        }
        //blah blah blah
    }
    public void method1(){

    }
}

And then create instance of the class --> set value of param1 --> then call method1.

Note: Looking at the constructor this class seems to be an extension to standard controller. so while creating instance of the class pass standard controller to the constructor.

1
  • should not param1 be static. To first set the value and then call constructor.
    – starhunter
    Commented Feb 9, 2017 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .