Loading

Salesforce Apex Managed Package: Exception.getStackTraceString() Does Not Expose Stack Trace in Asynchronous Context

Date de publication: Jun 9, 2026
Description

In Salesforce, when a managed package class calls Exception.getStackTraceString() and the managed package is invoked synchronously from outside the package (for example, via Execute Anonymous), the full stack trace is visible because the invoking user's namespace context is blank (non-managed). However, when the same managed package class is scheduled as an asynchronous operation (for example, via System.enqueueJob()), the stack trace is hidden — only empty parentheses "()" appear in the error details. This difference in behavior is by design.


The scenario typically involves three Apex classes: a Thrower class in the managed package (which implements the Queueable interface) that catches exceptions and rethrows using getStackTraceString(), a ThrowerTest class in the managed package that enqueues the Thrower, and a ThrowerService class outside the managed package whose constructor throws an exception.
 

Résolution

This behavior is working as designed. The key factor is the invoking user execution context at the time the transaction starts.

Why Synchronous Execution Shows the Full Stack Trace

In synchronous execution, when an administrator runs code via Execute Anonymous, the invoking namespace is blank (non-managed). The namespace stack is: blank → MANAGED_NAMESPACE → blank. Because the exception is thrown within the blank namespace and the invoking context is also blank, the platform allows the stack trace to be logged and displayed to the user.

Why Asynchronous Execution Hides the Stack Trace

In asynchronous execution, the Queueable job is enqueued from within the managed package. When the async job starts executing as a new transaction, the invoking user context originates from within the managed namespace. The namespace stack is: MANAGED_NAMESPACE → blank. The platform protects the managed package's proprietary implementation by only exposing stack trace information within the managed namespace's umbrella — even if the exception was thrown in the blank namespace. This obfuscation prevents the managed package from exposing proprietary implementation details to subscribers.

Workarounds

  1. Enable managed package debug logs to see the complete stack trace in the debug log results.
  2. Any exception handling and resolution must be implemented inside the managed package itself, since the managed package controls the transaction when it begins the async operation.
  3. Contact Salesforce Support to temporarily enable subscriber org logging if troubleshooting assistance is required.

Numéro d’article de la base de connaissances

000383964

 
Chargement
Salesforce Help | Article