Thursday, January 11, 2024

Dynamically detect the running context in salesforce using Apex

In the salesforce, the Request class provides a few methods among them we do have a getQuiddity () method that returns an enum(System.Quiddity) specifying the context of the origin of this request. 

Ex:

Quiddity contextInfo = Request.getCurrent().getQuiddity();
switch on contextInfo {
    when VF {
        system.debug('Visualforce');         
    } when AURA {
        system.debug('AURA');       
    } when FUTURE, SCHEDULED, QUEUEABLE, BATCH_APEX {
        system.debug('async context');        
    } when INVOCABLE_ACTION {
        system.debug('FLOW'); 
    } when ANONYMOUS {
        system.debug('ANONYMOUS');
    } when REST {
        system.debug('REST');        
    }
}
For Reference 

No comments:

Post a Comment