Skip to content

Instantly share code, notes, and snippets.

@jsullivanlive
Forked from kevinohara80/Async.cls
Created March 19, 2014 22:27

Revisions

  1. @kevinohara80 kevinohara80 revised this gist Feb 13, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Async.cls
    Original file line number Diff line number Diff line change
    @@ -134,7 +134,6 @@ public class Async {
    }
    return null;


    }

    }
  2. @kevinohara80 kevinohara80 revised this gist Feb 13, 2012. 2 changed files with 3 additions and 119 deletions.
    3 changes: 3 additions & 0 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -120,6 +120,7 @@ public class Async {
    } catch (Exception e) {
    System.debug('Error in SObject List serialization');
    }
    return null;

    }

    @@ -131,6 +132,8 @@ public class Async {
    } catch (Exception e) {
    System.debug('Error in SObject serialization');
    }
    return null;


    }

    119 changes: 0 additions & 119 deletions Async_Test.cls
    Original file line number Diff line number Diff line change
    @@ -1,119 +0,0 @@
    @isTest
    private class Async_Test {

    static List<Lead> leads;

    static {

    leads = new List<Lead>();

    Lead l1 = new Lead();
    l1.FirstName = 'Test1';
    l1.LastName = 'TestLast1';
    l1.Email = '[email protected]';
    l1.Status = 'New';
    l1.Company = 'Test Co';
    leads.add(l1);

    Lead l2 = new Lead();
    l2.FirstName = 'Test2';
    l2.LastName = 'TestLast2';
    l2.Email = '[email protected]';
    l2.Status = 'New';
    l2.Company = 'Test Co';
    leads.add(l2);

    }


    static testMethod void testInsertList() {
    Async.insertSObjects(Async.prepare(leads));
    }

    static testMethod void testInsertSingle() {
    Async.insertSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testUpdateList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.updateSObjects(Async.prepare(leads));
    }

    static testMethod void testUpdateSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.updateSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testUpsertList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.upsertSObjects(Async.prepare(leads));
    }

    static testMethod void testUpsertSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.upsertSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testDeleteList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.deleteSObjects(Async.prepare(leads));
    }

    static testMethod void testDeleteSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.deleteSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void forceSingleInsertFailure() {
    SObject s;
    Async.insertSObjects(Async.prepare(s));
    }

    static testMethod void forceListInsertFailure() {
    List<SObject> s = new List<SObject>();
    Async.insertSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleUpdateFailure() {
    SObject s;
    Async.updateSObjects(Async.prepare(s));
    }

    static testMethod void forceListUpdateFailure() {
    List<SObject> s = new List<SObject>();
    Async.updateSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleUpsertFailure() {
    SObject s;
    Async.upsertSObjects(Async.prepare(s));
    }

    static testMethod void forceListUpsertFailure() {
    List<SObject> s = new List<SObject>();
    Async.upsertSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleDeleteFailure() {
    SObject s;
    Async.deleteSObjects(Async.prepare(s));
    }

    static testMethod void forceListDeleteFailure() {
    List<SObject> s = new List<SObject>();
    Async.deleteSObjects(Async.prepare(s));
    }

    }
  3. @kevinohara80 kevinohara80 revised this gist Feb 13, 2012. 1 changed file with 119 additions and 0 deletions.
    119 changes: 119 additions & 0 deletions Async_Test.cls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    @isTest
    private class Async_Test {

    static List<Lead> leads;

    static {

    leads = new List<Lead>();

    Lead l1 = new Lead();
    l1.FirstName = 'Test1';
    l1.LastName = 'TestLast1';
    l1.Email = '[email protected]';
    l1.Status = 'New';
    l1.Company = 'Test Co';
    leads.add(l1);

    Lead l2 = new Lead();
    l2.FirstName = 'Test2';
    l2.LastName = 'TestLast2';
    l2.Email = '[email protected]';
    l2.Status = 'New';
    l2.Company = 'Test Co';
    leads.add(l2);

    }


    static testMethod void testInsertList() {
    Async.insertSObjects(Async.prepare(leads));
    }

    static testMethod void testInsertSingle() {
    Async.insertSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testUpdateList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.updateSObjects(Async.prepare(leads));
    }

    static testMethod void testUpdateSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.updateSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testUpsertList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.upsertSObjects(Async.prepare(leads));
    }

    static testMethod void testUpsertSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.upsertSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void testDeleteList() {
    insert leads;
    for(Lead l : leads) {
    l.FirstName = 'NewName';
    }
    Async.deleteSObjects(Async.prepare(leads));
    }

    static testMethod void testDeleteSingle() {
    insert leads;
    leads.get(0).FirstName = 'NewName';
    Async.deleteSObjects(Async.prepare(leads.get(0)));
    }

    static testMethod void forceSingleInsertFailure() {
    SObject s;
    Async.insertSObjects(Async.prepare(s));
    }

    static testMethod void forceListInsertFailure() {
    List<SObject> s = new List<SObject>();
    Async.insertSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleUpdateFailure() {
    SObject s;
    Async.updateSObjects(Async.prepare(s));
    }

    static testMethod void forceListUpdateFailure() {
    List<SObject> s = new List<SObject>();
    Async.updateSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleUpsertFailure() {
    SObject s;
    Async.upsertSObjects(Async.prepare(s));
    }

    static testMethod void forceListUpsertFailure() {
    List<SObject> s = new List<SObject>();
    Async.upsertSObjects(Async.prepare(s));
    }

    static testMethod void forceSingleDeleteFailure() {
    SObject s;
    Async.deleteSObjects(Async.prepare(s));
    }

    static testMethod void forceListDeleteFailure() {
    List<SObject> s = new List<SObject>();
    Async.deleteSObjects(Async.prepare(s));
    }

    }
  4. @kevinohara80 kevinohara80 revised this gist Feb 11, 2012. 1 changed file with 44 additions and 11 deletions.
    55 changes: 44 additions & 11 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    public class Async {

    /***********************/
    /* ASYNC CRUD METHODS */
    /* ASYNC DML METHODS */
    /***********************/

    // insert sobjects
    @@ -29,9 +29,14 @@ public class Async {
    }

    if(!sObjs.isEmpty()) {
    insert sObjs;
    try {
    insert sObjs;
    } catch (Exception e) {
    System.debug('Error inserting SObjects');
    }

    }

    }

    // upsert sobjects
    @@ -47,9 +52,14 @@ public class Async {
    }

    if(!sObjs.isEmpty()) {
    upsert sObjs;
    try {
    upsert sObjs;
    } catch (Exception e) {
    System.debug('Error upserting SObjects');
    }

    }

    }

    // update sobjects
    @@ -65,9 +75,14 @@ public class Async {
    }

    if(!sObjs.isEmpty()) {
    update sObjs;
    try {
    update sObjs;
    } catch (Exception e) {
    System.debug('Error updating SObjects');
    }

    }

    }

    // delete sobjects
    @@ -83,9 +98,14 @@ public class Async {
    }

    if(!sObjs.isEmpty()) {
    delete sObjs;
    try {
    delete sObjs;
    } catch (Exception e) {
    System.debug('Error deleting SObjects');
    }

    }

    }

    /***********************/
    @@ -94,11 +114,24 @@ public class Async {

    // list of sobjects
    public static String prepare(List<SObject> sObjs) {
    return JSON.serialize(sObjs);

    try {
    return JSON.serialize(sObjs);
    } catch (Exception e) {
    System.debug('Error in SObject List serialization');
    }

    }

    // single sobject
    public static String prepare(SObject sObj) {
    return JSON.serialize(new List<SObject>{sObj});

    try {
    return JSON.serialize(new List<SObject>{sObj});
    } catch (Exception e) {
    System.debug('Error in SObject serialization');
    }

    }

    }
  5. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,13 @@
    /*
    * 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 function. A helper method is also included for making the serialization
    * processes simpler.
    * into an asynchronous method for DML operations. A helper method is also included for making
    * the serialization processes simpler.
    */

    public class Async {
  6. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Async.cls
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    * Author: Kevin O'Hara
    * Date: 02/10/2012
    * Description: The Async class aims to circumvent a limitation of asynchronous (@future)
    * functions in Salesforce. The current @future implementation will only allow for primitives
    * 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 function. A helper method is also included for making the serialization
  7. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@

    /* Author: Kevin O'Hara
    /*
    * Author: Kevin O'Hara
    * Date: 02/10/2012
    * Description: The Async class aims to circumvent a limitation of asynchronous (@future)
    * functions 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
  8. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,13 @@

    /* Author: Kevin O'Hara
    * Description: The Async class aims to circumvent a limitation of asynchronous (@future)
    * functions 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 function. A helper method is also included for making the serialization
    * processes simpler.
    */

    public class Async {

    /***********************/
  9. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Usage
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ for(Lead l : lds) {
    l.FirstName = 'BLAHHH';
    }

    // 3. Use the helper method to serialize to JSON, then call the appropriate DML function
    // 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
  10. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,14 @@
    // update a couple of leads async
    // 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';
    }

    Async.updateSObjects(Async.prepare(lds));
    // 3. Use the 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
  11. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. No changes.
  12. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. No changes.
  13. @kevinohara80 kevinohara80 revised this gist Feb 10, 2012. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ public class Async {
    if(!sObjs.isEmpty()) {
    insert sObjs;
    }

    }

    // upsert sobjects
    @@ -36,6 +37,7 @@ public class Async {
    if(!sObjs.isEmpty()) {
    upsert sObjs;
    }

    }

    // update sobjects
    @@ -53,6 +55,7 @@ public class Async {
    if(!sObjs.isEmpty()) {
    update sObjs;
    }

    }

    // delete sobjects
    @@ -70,6 +73,7 @@ public class Async {
    if(!sObjs.isEmpty()) {
    delete sObjs;
    }

    }

    /***********************/
  14. @kevinohara80 kevinohara80 created this gist Feb 10, 2012.
    88 changes: 88 additions & 0 deletions Async.cls
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    public class Async {

    /***********************/
    /* ASYNC CRUD 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()) {
    insert sObjs;
    }
    }

    // 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()) {
    upsert sObjs;
    }
    }

    // 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()) {
    update sObjs;
    }
    }

    // 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()) {
    delete sObjs;
    }
    }

    /***********************/
    /* HELPER METHODS */
    /***********************/

    // list of sobjects
    public static String prepare(List<SObject> sObjs) {
    return JSON.serialize(sObjs);
    }

    // single sobject
    public static String prepare(SObject sObj) {
    return JSON.serialize(new List<SObject>{sObj});
    }
    }
    9 changes: 9 additions & 0 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // update a couple of leads async

    List<Lead> lds = [SELECT Id, FirstName, LastName FROM Lead LIMIT 2];

    for(Lead l : lds) {
    l.FirstName = 'BLAHHH';
    }

    Async.updateSObjects(Async.prepare(lds));