Skip to content

Instantly share code, notes, and snippets.

@eddywebs
Forked from quintonwall/AddressFuture
Created October 26, 2015 17:18
Show Gist options
  • Save eddywebs/a1ac7b79fd7f9af6d261 to your computer and use it in GitHub Desktop.
Save eddywebs/a1ac7b79fd7f9af6d261 to your computer and use it in GitHub Desktop.
Example of using JSON serialization to pass complex objects to @future handlers in Apex
/**
* Example of using JSON serialization to pass complex objects to @future handlers in Apex
* $author: [email protected]
*/
public with sharing class AddressFuture {
public AddressFuture () {
List<String> addresses = new List<String>();
AddressHelper ah1 = new AddressHelper('1 here st', 'San Francisco', 'CA', '94105');
AddressHelper ah2 = new AddressHelper('2 here st', 'San Francisco', 'CA', '94105');
AddressHelper ah3 = new AddressHelper('3 here st', 'San Francisco', 'CA', '94105');
//serialize my objects
addresses.add(JSON.serialize(ah3));
addresses.add(JSON.serialize(ah2));
addresses.add(JSON.serialize(ah3));
doFutureCall(addresses);
}
@future
static void doFutureCall(List<String> addressesSer) {
AddressHelper currAddress = null;
for (String ser : addressesSer)
{
currAddress = (AddressHelper) JSON.deserialize(ser, AddressHelper.class);
System.debug('Deserialized in future:'+currAddress.street);
}
}
}
/**
* Sample encapsulated class
* $author: [email protected]
*/
public with sharing class AddressHelper {
public String street {set; get;}
public String city {set; get;}
public String state {set; get;}
public String zip {set; get;}
public AddressHelper(String s, String c, String st, String z) {
street = s;
city = c;
state = st;
zip = z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment