The following scripts can be used to create or update Trace Flags in Salesforce. I like using these to initialize debug logs for my user without having to approach the Salesforce Web UI.
Tooling API Objects
- TraceFlag - Who is activated for logging and for how long?
- DebugLevel - What is logged, and in how much detail?
- ApexLog - The actual log.
Required Tools
- @salesforce/cli
- jq
- date
(Optional) Reset the logging in an environment
If you want to reset the logging environment to an original state, then using the Tooling API export these to a csv then delete them in the environment for a full reset.
SELECT Id
FROM TraceFlag
SELECT Id
FROM DebugLevel
SELECT Id
FROM ApexLog
Initial Setup
Create a DebugLevel that tracks everything in the finest detail available.
sf data create record \
--sobject DebugLevel \
-v "DeveloperName=ALL_FINEST MasterLabel=ALL_FINEST ApexCode=FINEST ApexProfiling=FINEST Callout=FINEST Database=FINEST Nba=FINE Validation=INFO Visualforce=FINER Wave=FINEST Workflow=FINER" \
--use-tooling-api
Create and Modifying TraceFlags
🚨 Assumptions:
- You are running these commands in a workspace that is authorized to a default org
- You have one user authorized to this default org
- You have a DebugLevel with DeveloperName=“ALL_FINEST” in the default org
- For the
EXP
variable, you may need to choose which lines execute depending on whichdate
tool is installed on your device. I’m on Mac OS, so I’m using the tool that works for me.
Create a TraceFlag for your user.
Run this if you DON’T already have a Trace Flag in your default org.
#!/bin/bash
# create-traceflag.sh
NOW=$(date +%Y-%m-%dT%H:%M:%S%z)
# Setup the EXP to expire after one hour.
# date - Linux Only
# EXP=$(date -d '+1 hour' +%Y-%m-%dT%H:%M:%S%z)
# date - Mac OS Only
EXP=$(date -v+1H +%Y-%m-%dT%H:%M:%S%z)
# Get your own UserId based on default org
TRACED_ENTITY_ID=$(sf org list users --json | jq -r '.result[0].userId')
# Get the DebugLevel by DeveloperName=ALL_FINEST
DEBUG_LEVEL_ID=$(sf data get record --sobject DebugLevel --where "DeveloperName=ALL_FINEST" --use-tooling-api --json | jq -r '.result.Id')
sf data create record \
--sobject TraceFlag \
-v "StartDate=$NOW ExpirationDate=$EXP TracedEntityId=$TRACED_ENTITY_ID LogType=USER_DEBUG DebugLevelId=$DEBUG_LEVEL_ID" \
--use-tooling-api
Update an Existing TraceFlag for your user.
Run this if you already have a TraceFlag for your user in your default org.
#!/bin/bash
# update-traceflag.sh
NOW=$(date +%Y-%m-%dT%H:%M:%S%z)
# Setup the EXP to expire after one hour.
# date - Linux Only
# EXP=$(date -d '+1 hour' +%Y-%m-%dT%H:%M:%S%z)
# date - Mac OS Only
EXP=$(date -v+1H +%Y-%m-%dT%H:%M:%S%z)
CURRENT_USER_ID=$(sf org list users --json | jq -r '.result[0].userId')
# Get an existing TraceFlag for your user.
TRACE_FLAG_ID=$(sf data get record --sobject TraceFlag --where "TracedEntityId=$CURRENT_USER_ID" --use-tooling-api --json | jq -r '.result.Id')
# Get the DebugLevel by DeveloperName=ALL_FINEST
DEBUG_LEVEL_ID=$(sf data get record --sobject DebugLevel --where "DeveloperName=ALL_FINEST" --use-tooling-api --json | jq -r '.result.Id')
sf data update record \
--sobject TraceFlag \
--record-id $TRACE_FLAG_ID \
-v "StartDate=$NOW ExpirationDate=$EXP DebugLevelId=$DEBUG_LEVEL_ID" \
--use-tooling-api