2

Does @RemoteAction have to be in a separate controller than your other methods?

Example; using something like below, returns a nullpointerexception in remote action.

public with sharing class clsGetViewData {
    public String something { get; set; }

    public clsGetViewData(){
        something = [Query].field;
    }

    public List<sObject> getSomeOtherData() {
        return [Query];
    }

    @RemoteAction
    public static List<sObject> MyRemoteAction() {
        return [Query];
    } 
}

But in the above code, removing all methods, getter/setter, etc and only leaving remote action works...

Note: I have used an extension but since it's a different question, I'll ask about it seperately.

3
  • Are you using any get set variable in remote action(MyRemoteAction)? Commented Jul 25, 2017 at 6:00
  • @AjayPrakashDubey No, simply returning results of query
    – Y12K
    Commented Jul 25, 2017 at 6:09
  • Can you post what "Query" you are making? Commented Jul 25, 2017 at 6:25

2 Answers 2

2

You can't access properties of the Controller inside Remote action method. Remote action is a Static Method only can be access static variable. If you wanted use a controller variable . You have to create instance of the Controller and through instance only you can access the variable with in remote action context only.

public with sharing class clsGetViewData {
    public String something { get; set; }

    public clsGetViewData(){
        something = [Query].field;
    }

    public List<sObject> getSomeOtherData() {
        return [Query];
    }

    @RemoteAction
    public static List<sObject> MyRemoteAction() {
       clsGetViewData Controller=nwe clsGetViewData ();
       String Value=Controller.something;
       List<sObject> listSObjects=Controller.getSomeOtherData();
       // your stuff's 
       return [Query];
    } 
}
1

In the Remote action method, you cannot use the variables of the Controller as it is a different method. Therefore, you are getting NullPointer Exception

You must log in to answer this question.

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