Provides an API wrapper around Salesforce SOQL/SOSL queries. Written in Apex.
Only supports basic query strings currently. Pull requests welcome.
- Open Salesforce Developer Console.
- Go to
File
>New
>Apex Class
. - Name the new class as
QueryBuilder
. - Copy the contents of
QueryBuilder.cls
into the new class. - Go to
File
>New
>Apex Class
again. - Name the new class as
QueryBuilderTest
. - Copy the contents of
QueryBuilderTest.cls
into the new class. - Click
Run Test
to makes sure the tests pass.
// Build a query:
QueryBuilder query = new QueryBuilder();
query.addSelectField('Id');
query.addSelectField('Name');
QueryBuilder subquery = new QueryBuilder();
subquery.addFromObject('Contacts');
subquery.addSelectField('Name');
query.addSelectField(subquery);
query.addFromObject('Case');
query.addWhereClause('IsActive = true');
query.addSortByField('Name');
query.setSortDESC();
String queryStr = query.getQuery();
// Outputs `SELECT Id, Name, (SELECT Name FROM Contacts) FROM Case WHERE IsActive = true SORT BY Name DESC`
See test class for other formats.