Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to query ApexLog raw body?

Asked by Alexander Coxon Sep 22, 2022 3.7K views 2 answers
Share

About this question

 I need to find, in Apex Logs raw bodies, references to a given String value.

I know we can query the object Apex Log fields, by using the following code: SELECT id, 

       loguserid, 

       loglength, 

       lastmodifieddate, 

       request, 

       operation, 

       application, 

       status,

       durationmilliseconds, 

       systemmodstamp, 

       starttime, 

       location 

FROM   apex log 

And, once we have the log ID, we can either retrieve a raw log, using the REST resource /objects/Apex Log/id/Body/ or even the totally not recommended trace download:

https://YourPrefix.salesforce.com/apexdebug/traceDownload.apexp?id=99999999999999YourID

Now, is it possible to query Debug Logs Bodies, directly, using a WHERE clause, to find a string reference? In which object the debug body is stored?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 28, 2024

Querying the raw body of ApexLog records in Salesforce is a bit different from querying other standard or custom objects due to the nature of the log data. Apex logs in Salesforce are stored as binary data in the LogFile field of the ApexLog object, which means you need to download the log file to read its contents.

Here's a step-by-step guide to query and retrieve the raw body of ApexLog records using the Salesforce REST API or the Developer Console:

Using the Developer Console

  • In Salesforce, click on your avatar (profile picture) in the top-right corner.
  • Select "Developer Console".

Run a SOQL Query:

  • In the Developer Console, go to the "Query Editor" tab.
  • Run the following query to get a list of ApexLog records:

SELECT Id, LogUser.Name, Operation, StartTime, Status FROM ApexLog ORDER BY StartTime DESC LIMIT 10

Download the Log File:

  • After running the query, you will see a list of ApexLog records.
  • Note the Id of the log record you want to examine.
  • Retrieve the Log Content:
  • ou can't directly read the LogFile field in the Developer Console. Instead, you can download the log file via the Salesforce Setup interface:
  • Go to Setup.
  • In the Quick Find box, type "Debug Logs" and select "Debug Logs".
  • Find the log entry corresponding to the Id you noted.
  • Click on "View" to download the log content.

Using the REST API

1. Authenticate with the Salesforce REST API:

Obtain an access token by authenticating with Salesforce using OAuth 2.0.

2. Query ApexLog Records:

Use the following endpoint to query ApexLog records:

GET /services/data/vXX.0/query/?q=SELECT+Id,LogUser.Name,Operation,StartTime,Status+FROM+ApexLog+ORDER+BY+StartTime+DESC+LIMIT+10

Replace vXX.0 with the appropriate API version.




3. Download the Log File:

Use the Id of the log record obtained from the query to download the log content.

  GET /services/data/vXX.0/sobjects/ApexLog/{LOG_ID}/Body

Replace {LOG_ID} with the actual Id of the ApexLog record.

Example Using cURL:

First, authenticate and get an access token (this example assumes you have your client credentials and instance URL):

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=password&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&username=YOUR_USERNAME&password=YOUR_PASSWORD" https://login.salesforce.com/services/oauth2/token
Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.