Activity Forums Salesforce® Discussions Why we can't pass Object either Standard or Custom in Future method as a Parameter in salesforce?

  • Saurabh

    Member
    April 10, 2017 at 12:27 pm

    Hello Suraj

    Reason for you can't pass Object either Standard or Custom in Future method as a Parameter

    The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record.
    And also There is no direct way to pass SObject record as a parameter in @future method, but you can make one hack where first you will serialize object state in string and pass this string to @future method and de-serialize  it again to convert it back in SObject.

    For Example

    trigger MyTrigger on Test_Object__c (after insert) {
    String objJSONStr = Json.serialize(Trigger.new[0]);

    MyFutureClass.myFutureMethod(objJSONStr);
    }
    public class MyFutureClass {
    @future
    public static void generateEtsPdfAttachment(String objJson){
    Test_Object__c obj = (Test_Object__c) Json.deserialize(objJson, System.Type.forName('Test_Object__c'));
    }
    }

    For more information you can lookup here:https://developer.salesforce.com/blogs/developer-relations/2013/06/passing-objects-to-future-annotated-methods.html

    Hope it may help you:

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos