[SalesForce] Return more than 2000 records (SOQL)

I'm trying to retrieve a list of Account objects from Salesforce using SOQL. Originally my query was this:

var result = service.query("SELECT Id, Name, RecordTypeId FROM Account");
return result.records.Select(record => record as Account).ToList();

However this only returns a maximum of 2,000 records and I have over that which I need to be returned by this query. After some digging around I found an example in Java which loops over the SOQL query which will allow a maximum of 10,000 records to be returned. I tried to replicate this in C# but it differs slightly, here is what I have now:

var accounts = new List<Account>();
foreach (Account account in service.query("SELECT Id, Name, Composer_ID__c, RecordTypeId FROM Account").records)
    accounts.Add(account);

return accounts;

However this still only returns a maximum of 2,000 records. How can I return more than 2,000 records by querying Account?

Best Answer

Seems you are using SOAP APIs, where by default the max records returned are 500, which could be uplifted to 2000. Best would be to use "queryMore" to load the records. Here is a post from salesforce docs with good code samples for the same: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_querymore.htm

Related Topic