How to query ApexLog raw body?

1.8K    Asked by AlexanderCoxon in Salesforce , Asked on Sep 22, 2022

 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?


Answered by alex Duncan

It's in Apex Log, but you can't query/filter the log itself, only the exposed metadata (e.g. status or request). You would necessarily need to download each one using the preferred API (/objects/Apex Log/{id}/Body). This limitation exists because you can't filter on the logs. You also cannot search (SOSL) on Apex Log, either, probably as a matter of performance.


Your Answer

Answer (1)

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
5 Months

Interviews

Parent Categories