+ Start a Discussion
sfdcksfdck 

Difference between synchronous and asynchronous in apex

Hi ,

 

     I am confused between synchronous and asynchronous can any one explain me from the scratch what are they actually , and when we use the @future annotation . Please Help me  !!!

 

 

Help is highly appreciated.

 

Thanks in Advance. 

ryanjuptonryanjupton

An aysch call is queued up in Salesforce and run on a separate thread of execution. At this point it becomes decoupled from the calling action. That also means the calling action won't block and wait for a response from the asycn call. Using the future annotation identifies methods that are executed asynchronously. They must also be static methods. There are some considerations to know when using aync methods, you can learn more here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

Vijay NagarathinamVijay Nagarathinam
@Future Annotation:
Use the future annotation to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce has available resources.

@Future (callout = true)
Specify the method to make the callout to external systems. (e.g. To execute callout method via an Apex trigger)

Further details:
Nitin Wader 21Nitin Wader 21
Synchronous Apex means entire Apex code is executed in one single go. It is like "Abhi ke Abhi"

Asynchronous apex is excuted when resources are available. So any calling method which calls Asynchronous apex wont wait for outcome of Asynchronous call. Calling method will go on further execution in code. And Asynchronous execution happens in sperate thread and then it will return to main program.

Above said is at very basic and rough level description.

For detailed one, refer SFDC documentation.
https://developer.salesforce.com/page/Asynchronous_Processing_in_Force_com
 
cloudstreamercloudstreamer
The Asynchronous apex is like any asynchronous process which means executing the task 'in the background'. Usually it is used in use cases where the process which invokes the asynchronous process does not need the task to finish immediately or is not waiting for it to complete before it can proceed with the next step in its execution path.
Asynchronous processes are queued in salesforce and are executed as soon as resources are available. But there is no guarantee when this will be executed. Hence not suitable for processes if some time sensitive action is planned based on the outcome of this process
Asynchronous processing includes @future Apex, batch Apex or scheduled apex.

Asynchronous processes have higher governer limits compared to synchronous apex.

@future annotation is used to define a future method (a method that runs asynchronously). The method must be static and return type of the method should be void and take primitive data types or arrays of primitive data types as arguments.
 
deepak balur 19deepak balur 19
Synchronous means user cannot perform other operations like clicling around on other views/screens/fields till the operation is completed. In asynchronous operations; the code is executed in the background and the user is free to perform other operations. @future/Batch and APEX jobs are ways of executing your code in asynchronous manner, they have their own pros/cons.
Cloud_forceCloud_force
Synchronous execution takes place as a single transaction and it does not wait for available resources. While asynchronous execution takes place depending on the resources available. @future, batch apex are examples of asynchronous execution. Governer limits in case of asynchronous are higher as compared to synchronous.
ruchika Nayyarruchika Nayyar
you can learn from here
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm