Activity Forums Salesforce® Discussions What is the return type of Future Method in Salesforce?

  • shradha jain

    Member
    August 3, 2018 at 5:01 am

    Hello Sanjana,

    The return type of Future Method can only be a void type.

    Thanks.

  • Parul

    Member
    September 9, 2018 at 6:54 pm

    Hello Sanjana,

    The return type of Future Method can only be a void type, that's why Future methods will run in the future. You don't want your synchronous code waiting an unknown period of time for an asynchronous bit of code to finish working.

    The whole point is you want to clearly separate what happens now from what happens in the future.

    By only returning void, you can't have code that waits for a result.

     

    Thanks.

  • shariq

    Member
    September 15, 2018 at 10:57 am

    Hi,

    Future Apex. Future Apex is used to run processes in a separate thread, at a later time when system resources become available. Note: Technically, you use the @future annotation to identify methods that run asynchronously.

    Methods with the future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the futureannotation cannot take sObjects or objects as arguments.

    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. The following example shows how to do so with a list of IDs.

    global class FutureMethodRecordProcessing
    {
    @future
    public static void processRecords(List<ID> recordIds)
    {
    // Get those records based on the IDs
    List<Account> accts = [SELECT Name FROM Account WHERE Id IN :recordIds];
    // Process records
    }
    }

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos