+ Start a Discussion
NARESH NALLURI 6NARESH NALLURI 6 

how to differentiate the synchronous and asynchronous while writing the trigger??

Head In CloudHead In Cloud
Hi Naresh,

Synchronous:

In a Synchronous call, the thread will wait until it completes its tasks before proceeding to next. In a Synchronous call, the code runs in single thread.

Example:

Trigger
Controller Extension
Custom Controller

Asynchronous:

In a Asynchronous call, the thread will not wait until it completes its tasks before proceeding to next. Instead it proceeds to next leaving it run in separate thread. In a Asynchronous call, the code runs in multiple threads which helps to do many tasks as background jobs.

Example:

Batch
@future Annotation

For further best prctices for trigger and Synchronous and Asynchronous you can refer the following links:
https://developer.salesforce.com/page/Apex_Code_Best_Practices
https://developer.salesforce.com/page/Governors_in_Apex_Code#Synchronous_vs_Asynchronous_Apex

Hope this helps.
 
Head In CloudHead In Cloud
Use @future Appropriately. Writing your Apex code well allows it to efficiently handle and process multiple records at a time, and the same applies to asynchronous Apex methods annotated with the @ future phrase. This makes is easy to find the difference between synchronous Apex and asynchronous Apex.
sfdcfoxsfdcfox
public static Boolean isAsync() { 
     return System.isBatch() || System.isQueueable() || System.isScheduled() || System.isFuture();
}
​
If this method returns true, you're in an asynchronous context; otherwise, you're not. You can put this in any class you like.
Vasani ParthVasani Parth
Synchronous communication works much like a phone call. The Receiver (callee) must be available, otherwise the conversation cannot occur.
Asynchronous communication works much like the postal system. The Receiver (callee) don't need to be available.

So, When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

Please mark this as the best answer if this helps