Use JSON serialization to get around parameter limitations for future methods?

Future methods can only take primitives or collections of primitives as parameters. To get around this you could serialize Sobjects (or classes) to a string and then deserialize them in the future method. Ex:

@future
public static void test(string foo)
{
    Contact con = json.deserialize(foo);
}

test(JSON.serialize(mycontact));

This seems to work but I’m wondering if anyone knows of any drawbacks or pitfalls to this approach?

Answer

A few thoughts:

  • If you are dealing with a collection the string resulting from JSON serialization could become prohibitively large. This could result in heap size limit issues during deserialization.
  • The data in the sObject may be stale by the time the future method executes. Fields may have been changed on the Contact before the future method runs.
  • You may be serializing and passing data that you otherwise don’t care about. E.g. You probably wouldn’t want to serialize a blob data field.

Weather any of these are an issue or not will entirely depend on your usage.

Attribution
Source : Link , Question Author : Greg Grinberg , Answer Author : Daniel Ballinger

Leave a Comment