In apex, I can write a query like:

[select id from contact where accountid in:accountIdSet]

If I use the Soap Api to do a query in external application(.Net), how can I do the same thing?

I know the Soql query length limit is 20000, if I build a query dynamically in the external application, it's very possible I pass the limit.

The only option I can think of now is to query more than once. Is there a better solution?

  • Did anyone ever figure out the answer to this question? – Lisa Apr 6 '16 at 18:02

If you're using the SOAP API and your query is that large, you're already going to be calling out more than once - you will have to use queryMore to get the records in batches like shown below. Using the SOAP API, no, there is no way to do that without more than one query.

You might be able to reduce the number of queries by creating an Apex REST or SOAP endpoint that takes your list of ids as a parameter or the body and sends back the result of the query as XML/JSON/whatever, but how many rows depends on the data you are querying: with Apex endpoints you are constrained by the size of the request and response body.


Use queryMore() with queryLocator which keeps a server-side cursor for your result set. Check the below C# code snippet is from documentation.

qResult = binding.query(soqlQuery);  //soqlQuery is your dynamic query and binding is your connection object

while (!done)
    sObject[] records = qResult.records;
    for (int i = 0; i < records.Length; ++i)
       Contact con = (Contact)records[i];
       String fName = con.FirstName;
       String lName = con.LastName;
       if (fName == null)
          Console.WriteLine("Contact " + (i + 1) + ": " + lName);
          Console.WriteLine("Contact " + (i + 1) + ": " + fName
                + " " + lName);
    if (qResult.done)
       done = true;
       qResult = binding.queryMore(qResult.queryLocator);

Hopefully the code itself explaining what it does clearly. The default result set (500 as I remember) also can be increased upto 2000 if you need as below.

binding.QueryOptionsValue.batchSize = 2000;
binding.QueryOptionsValue.batchSizeSpecified = true;
  • Can you read my question carefully please? – sfdcnoob Sep 15 '15 at 4:24
  • Yes your title doesn't match with what you asking :D. You mean the phrase you are saying "query more than once" is about queryMore() ? – highfive Sep 15 '15 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.