Detect Apex Runtime Context with RequestId and Quiddity

You can now detect Apex context at runtime and correlate multiple logs triggered by the request, using Request ID and Quiddity values. Use the methods in the System.Request class to obtain the Request ID and Quiddity of the current Salesforce request.

Where: This change applies to Lightning Experience and Salesforce Classic in Enterprise, Performance, Unlimited, and Developer editions.

How: First, use the Request.getCurrent() method to retrieve the Request object that contains the current Request ID and Quiddity. To retrieve the Request ID from the Request object, use the Request.getRequestId() method. To retrieve Quiddity and its short-code, use the Request.getQuiddity() and System.getQuiddityShortCode() methods.

The Request ID and Quiddity values can be used to effectively correlate logs:
  • The Request ID is universally unique and is captured in the debug logs triggered by the request.
  • The Request ID and Quiddity values are the same as in the event log files of the Apex Execution event type used in Event Monitoring.

Example

This example shows how to retrieve and use Request ID and Quiddity.

//Get info about the current request
Request reqInfo = Request.getCurrent();

//Universally unique identifier for this request
//Same as requestId in splunk or REQUEST_ID in event monitoring
String currentRequestId = reqInfo.getRequestId();


//enum representing how Apex is running. e.g. BULK_API vs LIGHTNING
//Use this with a switch statement,
//instead of checking System.isFuture() || System.isQueueable() || ...
Quiddity currentType = reqInfo.getQuiddity();