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