Loading
Feature degradation | Gmail Email delivery failureRead More
Enhance Salesforce with Code
Table of Contents
Select Filters

          No results
          No results
          Here are some search tips

          Check the spelling of your keywords.
          Use more general search terms.
          Select fewer filters to broaden your search.

          Search all of Salesforce Help
          Set Up Apex Class and Trigger Trace Flags

          Set Up Apex Class and Trigger Trace Flags

          You can set up an Apex class or trigger trace flag in Salesforce Setup, in the Developer Console, or by creating Tooling API objects with Salesforce CLI commands. These trace flags have the CLASS_TRACING debug log type and override the debug log levels of the USER_DEBUG and DEVELOPER_LOG trace flags.

          Required Editions

          Available in: Salesforce Classic (not available in all orgs) and Lightning Experience
          Available in: Enterprise, Performance, Unlimited, Developer, and Database.com Editions
          User Permissions Needed
          To use the Developer Console: API Enabled AND View All Data
          To view, retain, and delete debug logs: View All Data
          To execute anonymous Apex: Author Apex
          To save changes to Apex classes and triggers: Author Apex
          To use Salesforce CLI and run Tooling API queries: API Enabled
          Tip
          Tip For an applied example of using Apex class and trigger trace flags, see Debug Log Filtering for Apex Classes and Apex Triggers.

          Create Apex Class and Trigger Trace Flags in Setup

          Note
          Note Although you can set up an Apex class or trigger trace flag in Setup, these trace flags don’t appear in the log list on the Debug Logs page. The page shows only USER_DEBUG and DEVELOPER_LOG log types, not CLASS_TRACING log types. You can still access CLASS_TRACING log types created in Setup from the Apex class or trigger’s Setup page, from the Developer Console, or with Tooling API.
          1. From Setup, enter Debug Logs in the Quick Find box, and then click Debug Logs.
          2. Click New.
          3. For Traced Entity Type, select Apex Class or Apex Trigger.
          4. For Traced Entity Name, enter the name of the Apex class or trigger that you want to add a trace flag to.
          5. To define a time period during which the trace flag is active, enter a Start Date and an Expiration Date.
          6. To set a debug level profile, either enter the name of a debug level profile that you’ve already defined, or click New Debug Level.

            If you click New Debug Level, a new window opens. To define a new debug level profile, in the new window, enter a name for the debug level profile. Then select a debug log level for each debug log category. Save your changes.

          7. Save the trace flag.

          View, Edit, or Delete Apex Class and Trigger Trace Flags in Setup

          1. Navigate to the appropriate Apex class or trigger Setup page.
            1. For Apex class trace flags, from Setup, enter Apex Classes in the Quick Find box, and then click Apex Classes.
            2. For Apex trigger trace flags, from Setup, enter Apex Triggers in the Quick Find box, and then click Apex Triggers.
          2. Click the name of the Apex class or trigger that you want to examine.
          3. Click the desired option.
            1. To create a trace flag, click New. Then enter a start date, an expiration date, and a debug level profile for the trace flag.
            2. To delete a trace flag, in the trace flag’s Action column, click Delete.
            3. To modify a trace flag’s start date, expiration date, or debug level profile, click Edit.
            4. To modify a trace flag’s debug levels, click Filters. Then select the desired debug level for each category.
            5. To delete all trace flags for the Apex class or trigger, click Delete All.
          4. Save your changes.

          Create Apex Class and Trigger Trace Flags in the Developer Console

          1. From the Developer Console, click Debug | Change Log Levels….
            The Change Log Levels window in the Developer Console, where you can set debug trace flags.
          2. In the Class and Trigger Trace Overrides section, click Add.
          3. Select the class or trigger that you want to add a trace flag to.
          4. To define a time period during which the trace flag is active, click the Start Date cell for the class or trigger, and then enter or select a valid start time. Then click the Expiration Date cell and enter or select a valid end time.
          5. To set a debug level profile, click Add/Change in the DebugLevel Action cell for the class or trigger. Then perform one of these actions.
            1. To set the debug level to a debug level profile that you already defined, click the name of the debug level profile so that its row is highlighted. Then click Done.
            2. To adjust the debug levels of a debug level profile that you already defined, click the name of the debug level profile so that its row is highlighted. Select a new debug level from the dropdowns for each debug log category that you want to adjust. Then click Done.
            3. To add a new debug level profile, click Add. In the new window, enter a DebugLevel Name. Then select a debug level from the dropdowns for each debug log category. Click Add, and then click Done.
            4. To delete a debug level profile, click the name of the debug level profile, and then click Remove. Then click Done.
              Warning: This action removes the debug log profile from all of its assigned trace flags.
            The Change DebugLevel window in the Developer Console, where you can create and modify debug log level profiles.

          Create Apex Class and Trigger Trace Flags Using Salesforce CLI Commands

          Note
          Note Where possible, we changed noninclusive terms to align with our company value of Equality. We maintained certain terms to avoid any effect on customer implementations.
          1. If you don’t have an existing debug level profile that you want to use, first create a DebugLevel record with Tooling API by using the sf data create record command. Replace the placeholder values with the desired name of the debug level profile and the desired debug level for each debug log category.
            sf data create record --use-tooling-api --sobject DebugLevel --values "DeveloperName='DEBUG_LEVEL_PROFILE_NAME' MasterLabel='DEBUG_LEVEL_PROFILE_NAME' Workflow='FINER' ApexCode='FINE' System='DEBUG' Validation='INFO' Database='INFO' Callout='INFO' Visualforce='INFO' ApexProfiling='INFO'"
          2. Get the DebugLevel record ID for the debug level profile by using the sf data query command. Replace the placeholder value with the DeveloperName of the debug level profile.
            sf data query --query "SELECT Id FROM DebugLevel WHERE DeveloperName='DEBUG_LEVEL_PROFILE_NAME'"
          3. Get the ID of the Apex class or trigger that you want to trace by using the sf data query command. Replace the placeholder value with the name of the Apex class or trigger.
            sf data query --query "SELECT Id FROM ApexClass WHERE Name='APEX_CLASS_NAME'" 
          4. Create a TraceFlag record with Tooling API. Replace the placeholder values.
            sf data create record --use-tooling-api --sobject TraceFlag --values "TracedEntityId='APEX_CLASS_OR_TRIGGER_ID' DebugLevelId='DEBUG_LEVEL_ID' LogType='CLASS_TRACING' StartDate='START_TIME_UTC' ExpirationDate='END_TIME_UTC'"
          5. If you haven’t already, create another TraceFlag record on the user whose actions you want to trace. Apex class or trigger trace flags don’t generate debug logs on their own.
            sf data create record --use-tooling-api --sobject TraceFlag --values "TracedEntityId='USER_ID' DebugLevelId='DEBUG_LEVEL_ID' LogType='USER_DEBUG' StartDate='START_TIME_UTC' ExpirationDate='END_TIME_UTC'"

          To work with debug logs, use these commands:

          • To fetch the specified log or given number of most recent logs from the org, use the apex get log command.
          • To display a list of IDs and general information about debug logs, use the apex list log command.
          • To activate a debug log for the current user and stream the debug log to the console, use the sf apex tail og command.
           
          Loading
          Salesforce Help | Article