2

I was using simple-salesforce library to query the salesforce through REST API but getting query timeout error. I have not added the LIMIT keyword in the query as I thought salesforce will automatically only return 2000 result and after that, it will provide nextRecordURL for other 2000 records.

My Current implementation looks like this,

json_response = self.sf_api_instance.query_all_custom(query)
parent_mapper = SalesForceMapper(json_response, field_mapping=field_mapping) 
self.send_data(parent_mapper, object_type)
is_done = json_response.pop("done", False)
next_records_url = json_response.pop("nextRecordsUrl", '')
while True:
   if not is_done:
      json_response = self.sf_api_instance.query_more(next_records_url, True) 
      parent_mapper = SalesForceMapper(json_response, field_mapping=field_mapping) 
      self.send_data(parent_mapper, object_type)
      is_done = json_response.pop("done", False)
      next_records_url = json_response.pop("nextRecordsUrl", '')
   else:
      break

So is my understanding is right or I need to provide LIMIT in every query.

4

The beauty of query call of Rest API is it lets you query more than 50,000 rows which is a standard limit of APEX.

The reason being same Query API call is used in Reports and Data export stuff so no implicit Limit is added to it by the Salesforce platform.

It works in chunks of 2000. After the 1st iteration of 2000 records it will give you a query cursor. Using query cursor you can get next set of records.(eg:/services/data/v41.0/query/01g580Xag2q4gAtfAI-2000)

Thus you have to explicitly add a Limit clause to you SOQL or add filter conditions to get limited records.

You can test this consistent behaviour using workbench by using RestAPI Call. enter image description here

The only reason your query is timing out is that query is not selective.

It means you are not applying proper filter on your query.

| improve this answer | |
  • So you mean If the number of rows is about 1 million then I have to explicitly add the LIMIT clause in the query but if there is like 50k rows then salesforce will automatically provide us the nextRecordsUrl. – Sachin Aryal Mar 22 '18 at 12:28

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.