SOQL and Salesforce APIs
Published 22/08/2025
Salesforce APIs provide powerful tools for integrating, moving and migrating data across systems. SOQL (Salesforce Object Query Language) plays a crucial role in these processes, enabling efficient data retrieval and manipulation. There are many APIs and other ways of getting data so in this post, we’ll explore how SOQL is used in conjunction with Salesforce APIs, such as the REST API, Bulk API, SOAP API and Composite API, to facilitate seamless data integration and migration.
Understanding Salesforce APIs
Section titled “Understanding Salesforce APIs”Salesforce offers a vast variety of APIs to interact with its platform, each serving different purposes, we will focus on three that allow for SOQL queries:
- REST API: A lightweight and easy-to-use API for accessing Salesforce data and functionality over HTTP.
- Bulk API: Designed for handling large volumes of data, ideal for data migration and batch processing.
- SOAP API: A robust and feature-rich API that supports complex operations and is well-suited for enterprise-level integrations.
- Composite API: Allows you to execute a series of REST API requests in a single call, reducing the number of round-trips between the client and server.
Using SOQL with the REST API
Section titled “Using SOQL with the REST API”The REST API allows developers to execute SOQL queries to retrieve data from Salesforce. Here’s a basic example of how to use SOQL with the REST API:
Example Request:
Section titled “Example Request:”GET /services/data/vXX.X/query/?q=SELECT+Id,+Name+FROM+Account+WHERE+Industry='Technology' HTTP/1.1Host: yourInstance.salesforce.comAuthorization: Bearer {access_token}Explanation:
- The query is being sent to the query endpoint
/services/data/vXX.X/query, here a SOQL query can be executed and up to 2,000 records can be returned at a time in a synchronous request. - The SOQL query is included in the URL as a query parameter, specifically in the
q=parameter. - The
+symbol is used to represent spaces in the URL-encoded query string. For example,SELECT+Id,+Name+FROM+Accounttranslates toSELECT Id, Name FROM Account. - The
Authorizationheader contains the access token for authentication, ensuring that the request is securely authenticated and authorized to access Salesforce data.
Use Cases:
Section titled “Use Cases:”- Data Retrieval: Fetch data for integration with external systems or applications.
- Real-time Updates: Use SOQL queries to retrieve the latest data for real-time applications.
Using SOQL with the Bulk API
Section titled “Using SOQL with the Bulk API”The Bulk API is designed for handling large volumes of data efficiently, making it ideal for data migration and batch processing tasks. It allows you to process records asynchronously in batches, which is particularly useful when dealing with large datasets that exceed the limits of synchronous processing.
Key Features of the Bulk API:
Section titled “Key Features of the Bulk API:”- Asynchronous Processing: The Bulk API processes data in the background, allowing you to submit jobs and check their status later.
- Batch Processing: Data is processed in batches, which can be configured to optimize performance and resource usage.
- Scalability: Designed to handle millions of records, making it suitable for large-scale data operations.
Example Process for Using SOQL with the Bulk API:
Section titled “Example Process for Using SOQL with the Bulk API:”Create a Job: Initiate a job to define the operation you want to perform. For querying, you specify the operation type as query.
{ "operation": "query", "object": "Account", "contentType": "CSV"}Submit a Query: Use SOQL to define the data you want to retrieve. The query is submitted as part of the job.
{ "query": "SELECT Id, Name FROM Account WHERE Industry = 'Technology'"}Monitor Job Status: Check the status of the job to determine when it is complete. You can poll the job status or set up a callback to be notified upon completion.
Retrieve Results: Once the job is complete, download the results in batches. The results are typically returned in CSV format, which can be processed further as needed.
- Data Migration: Efficiently migrate large volumes of data between Salesforce and other systems, ensuring data integrity and consistency.
- Batch Processing: Perform batch updates, inserts, or deletions on large datasets without hitting governor limits.
Using SOQL with the SOAP API
Section titled “Using SOQL with the SOAP API”The SOAP API provides a robust framework for integrating Salesforce with enterprise systems. Here’s how SOQL is used with the SOAP API:
Example Request:
Section titled “Example Request:”<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com"> <soapenv:Header> <urn:SessionHeader> <urn:sessionId>your_session_id</urn:sessionId> </urn:SessionHeader> </soapenv:Header> <soapenv:Body> <urn:query> <urn:queryString>SELECT Id, Name FROM Account WHERE Industry = 'Technology'</urn:queryString> </urn:query> </soapenv:Body></soapenv:Envelope>Explanation:
- The SOQL query is embedded within the SOAP envelope.
- The
SessionHeadercontains the session ID for authentication.
Use Cases:
- Enterprise Integrations: Connect Salesforce with other enterprise systems using a standardized protocol.
- Complex Operations: Perform complex data operations that require robust error handling and transaction support.
Using SOQL with the Composite API
Section titled “Using SOQL with the Composite API”The Composite API allows you to execute multiple REST API requests in a single call, which can include SOQL queries. This reduces the number of round-trips between the client and server, improving performance and efficiency.
Example Request:
Section titled “Example Request:”{ "compositeRequest": [ { "method": "GET", "url": "/services/data/vXX.X/query/?q=SELECT+Id,+Name+FROM+Account+WHERE+Industry='Technology'", "referenceId": "AccountQuery" }, { "method": "GET", "url": "/services/data/vXX.X/sobjects/Contact/describe", "referenceId": "ContactDescribe" } ]}Explanation:
- The
compositeRequestarray contains multiple requests, including a SOQL query. - Each request has a
referenceIdthat can be used to refer to the response in subsequent requests. This allows you to run two queries where the output of the first query is used in the second.
Use Cases:
- Batch Operations: Perform multiple operations in a single API call, reducing latency.
- Data Synchronization: Synchronize data between Salesforce and other systems efficiently.
Performance and Security Considerations
Section titled “Performance and Security Considerations”- Governor Limits: Be aware of Salesforce’s governor limits when using APIs, especially with large datasets.
- Authentication: Ensure secure authentication using OAuth tokens or session IDs to protect data access.
- Data Privacy: Implement data privacy measures, such as field-level security and encryption, when integrating data.
Best Practices for Using SOQL with APIs
Section titled “Best Practices for Using SOQL with APIs”- Optimize Queries: Use indexed fields and selective filters to improve query performance.
- Error Handling: Implement robust error handling to manage API call failures and exceptions.
- Monitor API Usage: Keep track of API usage limits to avoid hitting quotas and ensure smooth operation.
Conclusion
Section titled “Conclusion”SOQL, when used in conjunction with Salesforce APIs, provides a powerful mechanism for data integration and migration. By understanding the capabilities and best practices of the REST, Bulk, SOAP, and Composite APIs, you can create efficient and secure solutions that enhance data connectivity and operational efficiency. With these tools, Salesforce becomes a versatile platform for managing and integrating data across diverse systems.