Skip to content

Instantly share code, notes, and snippets.

@kevinohara80
Created February 10, 2012 16:50
Show Gist options
  • Save kevinohara80/1790817 to your computer and use it in GitHub Desktop.
Save kevinohara80/1790817 to your computer and use it in GitHub Desktop.
Pass sObjects into future methods for DML - Salesforce
/*
* Author: Kevin O'Hara
* Date: 02/10/2012
*
* Description: The Async class aims to circumvent a limitation of asynchronous (@future)
* methods in Salesforce. The current @future implementation will only allow for primitives
* or collections of primitives to be passed into an @future method. The Async class uses
* native JSON serialization/deserialization to allow for passing an SObject, or List<SObject>
* into an asynchronous method for DML operations. A helper method is also included for making
* the serialization processes simpler.
*/
public class Async {
/***********************/
/* ASYNC DML METHODS */
/***********************/
// insert sobjects
@future
public static void insertSObjects(String jsonString) {
List<SObject> sObjs = new List<SObject>();
try {
sObjs = (List<SObject>) JSON.deserialize(jsonString, List<SObject>.class);
} catch (Exception e) {
System.debug('Error in JSON deserialization');
}
if(!sObjs.isEmpty()) {
try {
insert sObjs;
} catch (Exception e) {
System.debug('Error inserting SObjects');
}
}
}
// upsert sobjects
@future
public static void upsertSObjects(String jsonString) {
List<SObject> sObjs = new List<SObject>();
try {
sObjs = (List<SObject>) JSON.deserialize(jsonString, List<SObject>.class);
} catch (Exception e) {
System.debug('Error in JSON deserialization');
}
if(!sObjs.isEmpty()) {
try {
upsert sObjs;
} catch (Exception e) {
System.debug('Error upserting SObjects');
}
}
}
// update sobjects
@future
public static void updateSObjects(String jsonString) {
List<SObject> sObjs = new List<SObject>();
try {
sObjs = (List<SObject>) JSON.deserialize(jsonString, List<SObject>.class);
} catch (Exception e) {
System.debug('Error in JSON deserialization');
}
if(!sObjs.isEmpty()) {
try {
update sObjs;
} catch (Exception e) {
System.debug('Error updating SObjects');
}
}
}
// delete sobjects
@future
public static void deleteSObjects(String jsonString) {
List<SObject> sObjs = new List<SObject>();
try {
sObjs = (List<SObject>) JSON.deserialize(jsonString, List<SObject>.class);
} catch (Exception e) {
System.debug('Error in JSON deserialization');
}
if(!sObjs.isEmpty()) {
try {
delete sObjs;
} catch (Exception e) {
System.debug('Error deleting SObjects');
}
}
}
/***********************/
/* HELPER METHODS */
/***********************/
// list of sobjects
public static String prepare(List<SObject> sObjs) {
try {
return JSON.serialize(sObjs);
} catch (Exception e) {
System.debug('Error in SObject List serialization');
}
return null;
}
// single sobject
public static String prepare(SObject sObj) {
try {
return JSON.serialize(new List<SObject>{sObj});
} catch (Exception e) {
System.debug('Error in SObject serialization');
}
return null;
}
}
// EXAMPLE
// 1. Query some leads
List<Lead> lds = [SELECT Id, FirstName, LastName FROM Lead LIMIT 2];
// 2. Make some changes
for(Lead l : lds) {
l.FirstName = 'BLAHHH';
}
// 3. Use the prepare() helper method to serialize to JSON, then call the appropriate DML function
Async.updateSObjects(Async.prepare(lds));
// RESULT: Your DML is offloaded to an async request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment