Using the Enterprise WSDL, is it possible to perform two seperate SOQL queries in one batch?

For example: I would like to get both an opportunity and the contact in the primary contact role of that opportunity. I have two separate SOQL queries to do this. In an effort to conserve API calls, I would like to execute these in batch. Is this possible?

1 Answer 1


Yes, using a normal sub-query will result in those records appearing in the final results. The specific language you're using will affect the outcome, but generally speaking, the sub-queries will appear as arrays on the parent object. For example, the following code should work:

QueryResult query = client.query("SELECT Id, Name, (SELECT Id, Name FROM OpportunityContactRoles) FROM Opportunity WHERE Id = 'some_id'");

do {
    for(Opportunity opp:(Opportunity[])query.records) {
        for(OpportunityContactRole role:(OpportunityContactRole[])opp.OpportunityContactRoles.records) {
            // Do something for each contact role on this opportunity
    if(query.done) {
    query = client.queryMore(query.queryLocator);
} while(true);

The base class, called the QueryResult, contains a boolean "done", a "queryLocator" (cursor), an integer for "size", and an array of SObject records. Sub-queries can be advanced with queryMore if more than (maximum batch size) records are in the list, but those cursors are discarded when queryMore is called on the parent query, if applicable.

Also, cursors timeout after 15 minutes, so your process may want to use one thread for retrieving records and another for processing those results. Note that multi-threaded queries are not supported, so you shouldn't try to use the same queryLocator in multiple threads.

All normal SOQL rules apply in terms of number of relationships you can traverse, total number of objects involved, maximum query size in characters, and so on. Additional limits may apply to non-administrators, including the maximum number of rows returned, and some queries impose their own limits on usage (those are clearly documented on the appropriate objects where these limits occur).

I suggest you try importing the WSDL and using auto-complete hints to see what additional functionality may be available in your specific language.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .