[SalesForce] Visualforce Remoting Error

I am using Visualforce remoting to send data to a Remote Action method in my controller. The Remote Action method has a Map<String, Object> args parameter.

In the Remote Action method if I try to obtain a Map from the args map, then I get a "Visualforce Remoting Exception: undefined" error.

In other words, on the javascript side I have added an object to my params object:

params.myMap = {isActive: true, isNew: true};
params.nonMapValue = color: 'blue';

I do not get an error in the remote action until I try and obtain the value of the myMap key from the Map.

It is strange because I can put the map into system.debug and it will print out the whole map fine e.g.

{myMap={isActive=true, isNew=true}, nonMapValue=blue}

but I get an error when I try:

arguments.get('myMap');

Does anyone know why this is? This is not a casting issue because I am not trying to cast it yet.

Can anyone suggest a good way to be able to send over and use a javascript object to my Remote Action method?

Thanks

Best Answer

I got around this error by making the parameter of my Remote Action method a class which I define. Without changing the javascript in any way, the map is now correctly parsed and can be obtained as shown below.

My Class

public class MyClass {
    private String myNonMapValue;
    private Map<String, Boolean> myMap;

    public void setMyNonMapValue(String nmv) {
        this.nonMapValue = nmv;
    }

    public String getMyNonMapValue() {
        return nonMapValue;
    }

    public void setMyMap(Map<String, Boolean> m) {
        this.myMap = m;
    }

    public Map<String, Boolean> getMyMap() {
        return myMap
    }
}

My Remote Action Method

@RemoteAction
public static List<String> doWork(MyClass params) {

    String nonMapValue = params.getMyNonMapValue();
    Map<String, Boolean> myMap = params.getMyMap();
}
Related Topic