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

    public inherited sharing class GetQuiddity {
     public static Quiddity demonstrateGetQuiddity() {
         return Request.getCurrent().getQuiddity();
     }
    }
    
    
  2. Now, let's invoke this from one of our class which gets called from Aura Component and see what is returned.

    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];
     }
    }
    
    
  3. 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.

    https://cdn.hashnode.com/res/hashnode/image/upload/v1615997957502/y77eI56xC.png

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:

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;
        }
    }

To view some more Quiddity Recipes, refer to the apex-recipes on Github.

Reference :

Receive my updates

Email me on neha.kumari@hey.com, or follow me via RSS, Twitter or Instagram.