[SalesForce] unsupported parameter in future method

I am trying to create a future method and pass in the following parameter:

@future
private static void myMethod(Map<Id, List<String>> userMap) {

I get unsupported parameter type.

I can do:

@future
private static void myMethod(Map<Id, String> userMap) {

I know I can't pass objects, but why is a Map with a list of strings no longer accepted?

Is there a way I can do this using JSON serialize/deserialize?

Thanks.

Best Answer

Future method parameters are limited to primitives and collections of primitives. You're attempting to pass in a collection of collections, which is not supported.

You're correct that you can use JSON to get around this. This should put you on the right track:

// populate this however you were previously
Map<Id, List<String>> userMap = new Map<Id, List<String>>();

// serialize your data before calling the future method
myMethod(JSON.serialize(userMap));

@future
private static void myMethod(String userMapSerialized) {
    // deserialize back into our map of lists
    Map<Id, List<String>> userMap = (Map<Id, List<String>>)JSON.deserialize(userMapSerialized, Map<Id, List<String>>.class);

    // and away we go...

}

A more detailed example can be found here: Passing Objects to Future Annotated Methods.

Also, please take a look at this question regarding the potential implications of using this technique.