InForce CLI Tool - Complete Documentation
Salesforce REST Export - Module Documentation
Comprehensive documentation for SOQL export operations via REST API.
Page 1
InForce CLI Tool - Complete Documentation
Table of Contents
1. Introduction
2. Function Overview
2.1 export_soql
3. Detailed Workflow
4. Arguments Explained
5. Expected Output
6. Error Handling
7. Best Practices
8. Example Usage
Page 2
InForce CLI Tool - Complete Documentation
1. Introduction
The `rest_ops.py` module provides functionality for exporting Salesforce data to a CSV file using
SOQL queries via the REST API. It is designed to be lightweight, efficient, and handle query errors
gracefully.
2. Function Overview
2.1 export_soql
Main function of this module.
Signature:
export_soql(conn, soql_query: str, output_path: str)
Description:
Executes a SOQL query using a valid Salesforce connection and exports the result to a CSV file.
3. Detailed Workflow
1. Calls `query_all()` on the Salesforce connection object using the given SOQL query.
2. Catches malformed queries using `SalesforceMalformedRequest`.
3. Extracts records from the response, omitting metadata like 'attributes'.
4. Writes a CSV file with fieldnames derived from the first record.
5. Each record is written row by row.
This function uses UTF-8 encoding to support special characters in the exported data.
4. Arguments Explained
Page 3
InForce CLI Tool - Complete Documentation
- conn: A valid authenticated Salesforce connection object.
- soql_query: A string containing a valid SOQL statement.
- output_path: The file path where the resulting CSV will be saved.
5. Expected Output
- A CSV file saved to `output_path` containing the results of the SOQL query.
- Column headers will match the selected fields in the SOQL query.
- Nested metadata (like 'attributes') will be excluded.
- If no records are found, a message "No records Found" is printed and no file is created.
6. Error Handling
Handles malformed SOQL queries using a try/except block.
Example Error:
SalesforceMalformedRequest: [{'message': 'unexpected token:', 'errorCode':
'MALFORMED_QUERY'}]
Fix:
Ensure the SOQL syntax is correct, fields exist, and API names are case-sensitive.
7. Best Practices
- Validate SOQL queries in Salesforce Workbench before running them.
- Always sanitize or validate output paths to avoid file overwrite or path issues.
- Log the query and record count if integrating into larger systems.
Page 4
InForce CLI Tool - Complete Documentation
- Avoid SELECT *; always specify the fields you need.
8. Example Usage
from simple_salesforce import Salesforce
from rest_ops import export_soql
sf = Salesforce(username='user', password='pass', security_token='token')
query = "SELECT Id, Name FROM Account WHERE Industry = 'Technology'"
export_soql(sf, query, "tech_accounts.csv")
This will export all Account records with Industry = 'Technology' to the file `tech_accounts.csv`.
Page 5