function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul.SharmaRahul.Sharma 

Calling synchronous method inside a future method

Hi,
I need to know what will happen when a synchronous method called inside a fututre method. Will it run synchronously or it will also start its execution in asynchronous manner. Also please tell me what will happen if the called method is having any return type?
Thanks
Example:
@future
static void myFutureMethod(List<String> params) {
    // call synchronous method
    mySyncMethod(params);
}
Best Answer chosen by Rahul.Sharma
v varaprasadv varaprasad
Hi Rahul,

Yes, We can call a synchronous method inside a future method.
Both will run on Asynchronous Only.

Please check sample code below : 
 
==========Future Class===========

public class FutureAsyncClass {
    
    @Future
    public static void callSynchronus(){
        string accname = 'Varaprsad';
        SynchronusClass.inserAcc(accname);
    }


}

=======Synchronus Class =========

public class SynchronusClass {
    
    public static void inserAcc(string accName){
        Account acc = new Account();
        acc.name = accName;
        insert acc;
        system.debug('==acc=='+acc);
        
    }

}

===================

For confirmation please check debug logs.

Hope it helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad


 

All Answers

v varaprasadv varaprasad
Hi Rahul,

Yes, We can call a synchronous method inside a future method.
Both will run on Asynchronous Only.

Please check sample code below : 
 
==========Future Class===========

public class FutureAsyncClass {
    
    @Future
    public static void callSynchronus(){
        string accname = 'Varaprsad';
        SynchronusClass.inserAcc(accname);
    }


}

=======Synchronus Class =========

public class SynchronusClass {
    
    public static void inserAcc(string accName){
        Account acc = new Account();
        acc.name = accName;
        insert acc;
        system.debug('==acc=='+acc);
        
    }

}

===================

For confirmation please check debug logs.

Hope it helps you.
Please let me know in case of any other assistance.


Thanks
Varaprasad


 
This was selected as the best answer
Rahul.SharmaRahul.Sharma
Thanks @varaprasad ,
What error will occur if there is a return type in synchronus method?
v varaprasadv varaprasad
Hi Rahul,

Future method return type is void only.
But synchronous method will contain any return type.

It will not through any error.

Please check once below code : 
 
public class FutureAsyncClass {
    
    @Future
    public static void callSynchronus(){
        string accname = 'Varaprsad';
        id accid = SynchronusClass.inserAcc(accname);
        system.debug('==accid=='+accid);
    }

}

===========
public class SynchronusClass {
    
    public static id inserAcc(string accName){
        Account acc = new Account();
        acc.name = accName;
        insert acc;
        system.debug('==acc=='+acc);
        
        return acc.Id;
    }

}

Please execute code in the anonymous window and check once.

User-added image



Synchronous
In simple terms you can assume Synchronous as Sequential. In Synchronous process the thread waits for the task to be completed and then moves to the next task Sequentially. All the tasks are completed in a single thread.

The common example of a Synchronous apex is Trigger.


Asynchronous
In Asynchronous apex the thread does not waits for the task to be completed to move on to the next task. The tasks are run in different threads all together. These threads run in independent silos whenever the system is free.

I hope it helps you.
Please let me know in case of any other help.

If above information helps you please mark it as the best answer.


Thanks
Varaprasad
 
priyanshu narayanpriyanshu narayan
Hi Varaprasad,

Will both of them will be considered as separate transaction or not?