The Inut string that accepts would be JSON formated string.
public class Async
{
/***********************/ /* ASYNC DML METHODS */ /***********************/
// insert sobjects
@future
public static void insertSObjects(String jsonString)
{
List<SObject
try
{
sObjs = (List<SObject
}
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
try
{
sObjs = (List<SObject
}
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
try
{
sObjs = (List<SObject
}
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
try
{
sObjs = (List<SObject
}
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
{
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
}
catch (Exception e)
{
System.debug('Error in SObject serialization');
}
return null;
}
}
Usage // 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
No comments :
Post a Comment