Prompt Details
Model
Chat - (gpt-5.5)
Token size
254
Example input
[Insert]: Retrieve all active Accounts with at least one Closed Won Opportunity created this year for a sales dashboard.
[Insert ]: Account, Opportunity
[ Insert]: Account is Active Opportunity Stage = Closed Won Opportunity Close Date = Current Year
[Performance / Reporting / Automation / Integration / Other]: Reporting
[Speed / Governor Limits / Readability / Scalability / Other]: Speed
Example output
A. Requirement Analysis
Business Objective
Create a fast, optimized query that returns active customer accounts with successful sales this year.
Data Retrieval Needs
Account Name
Industry
Annual Revenue
Related Opportunity Name
Opportunity Amount
Opportunity Close Date
Assumptions & Constraints
Active accounts use a custom checkbox Active__c.
Data volume is large.
Query may run from Apex or a reporting integration.
Standard Salesforce governor limits apply.
B. Optimized SOQL Query
SQL
/* Retrieve Active Accounts with Closed Won Opportunities */
SELECT
Id,
Name,
Industry,
AnnualRevenue,
(
SELECT
Id,
Name,
Amount,
CloseDate
FROM Opportunities
WHERE
StageName = 'Closed Won'
AND CloseDate = THIS_YEAR
ORDER BY CloseDate DESC
)
FROM Account
WHERE
Active__c = TRUE
ORDER BY Name
LIMIT 500
Key Optimization Notes
Filters parent records before loading child records.
Uses Salesforce date literal (THIS_YEAR).
Applies LIMIT to reduce unnecessary data retrieval.
Retrieves only required fields.
C. Performance Review
Filter Optimization
Use indexed fields whenever possible.
Filter Active Accounts before loading Opportunities.
Indexing Considerations
Id is indexed.
Consider indexing Active__c if highly selective.
Standard indexed fields generally perform better.
Selectivity Observations
Highly selective filters improve execution speed.
Avoid querying all Accounts without filters.
Governor Limit Considerations
Minimize returned fields.
Avoid nested loops over large datasets.
Use pagination for very large result sets.
D. Best Practice Recommendations
Readability
Format query with one field per line.
Group related fields together.
Add comments in Apex where appropriate.
Maintainability
Replace hard-coded values with variables in Apex.
Store reusable filters in constants.
Security
Enforce object and field-level security.
Use WITH SECURITY_ENFORCED when appropriate.
Respect sharing rules in Apex.
E. Alternative Approaches
Relationship Query
SQL
SELECT
Name,
(
SELECT Name, Amount
FROM Opportunities
WHERE StageName = 'Closed Won'
)
FROM Account
WHERE Active__c = TRUE
Aggregate Query
SQL
SELECT
AccountId,
SUM(Amount),
COUNT(Id)
FROM Opportunity
WHERE
StageName = 'Closed Won'
AND CloseDate = THIS_YEAR
GROUP BY AccountId
Scalability Recommendations
Use Batch Apex for millions of records.
Use QueryLocator for bulk processing.
Implement pagination with OFFSET or keyset pagination where appropriate.
F. Validation Checklist
Functional Testing
✅ Verify only active Accounts are returned.
✅ Confirm only Closed Won Opportunities are included.
✅ Validate date filtering for the current year.
✅ Check all required fields are populated.
Performance Review
✅ Review Query Plan for selectivity.
✅ Ensure indexed filters are used.
✅ Monitor execution time with production-scale data.
✅ Verify governor limits are not exceeded.
Deployment Considerations
✅ Test in a sandbox with realistic data volumes.
✅ Validate user permissions and field access.
✅ Include Apex unit tests if the query is used in code.
✅ Monitor performance after deployment and refine filters if necessary.
By purchasing this prompt, you agree to our terms of service
GPT-5.5
Efficient SOQL queries are essential for scalable Salesforce applications.
Poorly written queries can lead to:
Slow performance
Governor limit issues
Excessive resource usage
Inefficient filtering
Difficult maintenance
Reduced application scalability
Well-optimized SOQL queries help developers:
Retrieve only necessary data
Improve execution performance
Reduce governor limit consumption
Enhance maintainability
Support scalable application design
...more
Added 1 week ago
