How to dynamically determine Calling Context using Apex in Salesforce?

How to dynamically determine Calling Context using Apex in Salesforce?

Batch – System.isBatch()
@future – System.isFuture()
Queueable – System.isQueueable()
Schedulable – System.isScheduled()
Trigger – Trigger.isExecuting

Visualforce – ApexPages.currentPage() != null
Apex REST – RestContext.request != null

Sample code to execute in Developer Console

system.debug('Batch is ' + System.isBatch());
system.debug('@future is ' + System.isFuture());
system.debug('Queueable is ' + System.isQueueable());
system.debug('Schedulable is ' + System.isScheduled());
system.debug('Trigger is ' + Trigger.isExecuting);
system.debug('Visualforce is ' + ApexPages.currentPage());
system.debug('Apex REST is ' + RestContext.request);

Output:

Leave a Reply