Quiddity in Salesforce

I stumbled upon this video where I came across the concept called Quiddity in Salesforce. What a complex word, I first thought. But, on understanding this, I figured out that it's such a useful feature to have.
We all have come across the System methods like System.isFuture(), System.isBatch(), etc. It returns the kind of process than invokes the current executing Apex. Like System.isFuture will return true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
Unlike System methods like isFuture(), isBatch(), etc., Quiddity offers many other values. It's a system defined Enum that tells you what caused your trigger to run. The complete list of Quiddity values can be found here. Quiddity provides a much more granular check when compared to System methods.
Now, let's quickly get into the code implementation of Quiddity in our Apex.
  1. Getting the Quiddity Value
    1. public inherited sharing class GetQuiddity {
       public static Quiddity demonstrateGetQuiddity() {
           return Request.getCurrent().getQuiddity();
       }
      }
      
  1. Now, let's invoke this from one of our class which gets called from Aura Component and see what is returned.
    1. public with sharing class fetchAccount {
       @AuraEnabled
       public static List<Account> getAccount(){
           Quiddity q = GetQuiddity.demonstrateGetQuiddity();
           system.debug('**Quiddity Value: '+q);
           return [Select Id, Name, Type, Phone from Account Limit 10];
       }
      }
      
  1. I'm calling the above apex class from a simple Aura Component to display the list of accounts. I won't add the code for that here, coz it's simple and could be found anywhere. Let's see the value for the debug statement in Developer Console.
    1. notion image
It returns the value "AURA" because the method is invoked from an Aura Component.
What we saw above, was a very simple implementation to fetch the Quiddity value of the apex method only.
Quiddity can have a broad range of use cases, especially to make our code most robust and secure. Some of the use cases are:
  • Run specific set of code only when invoked through certain set of jobs.
public void withQuiddity(){
        Quiddity q = System.Request.getCurrent().getQuiddity();
        public static List<Quiddity> allowedQuiddities = new List<Quiddity>{
            Quiddity.QUICK_ACTION,
            Quiddity.AURA
        };
        if(allowedQuiddities.contains(q)){
             //do something
        } else {
             return null;
        }
    }
  • Execute SOQL statements in with sharing / without sharing context based on where is it invoked from.
    •   public void quidditySecurityGuard(){
            public static List<Quiddity> trustedQuiddities = new List<Quiddity>{
                   Quiddity.QUEUEABLE,
                   Quiddity.BATCH_APEX,
                    Quiddity.RUNTEST_ASYNC,
                    Quiddity.RUNTEST_DEPLOY,
                    Quiddity.RUNTEST_SYNC
            };
            Quiddity q = System.Request.getCurrent().getQuiddity();
            if(trustedQuiddities.contains(q)){
                List<Account> accs = [Select ID from Account];
            } else{
                List<Account> accs = [Select Id from Account with SECURITY_ENFORCED];
            }
        }
      
To view some more Quiddity Recipes, refer to the apex-recipes on Github.
Reference :