0

This question already has an answer here:

Problem

I'm trying to make a standard query to get all records from salesforce custom object. I'm using SOAP API.

I have more than 40k records in object.

But salesforce OFFSET limit 2000 record. which is give error.

So, I have try out some workaround solution,

SELECT Id, Name, CreatedDate FROM Account WHERE CreatedDate > "Last-Returned-Created-Date" ORDER BY CreatedDate LIMIT 2000

But this query not work, if there was more than one object created at that exact date and time.

Question

Is there any way to do this?

Actually i am implementing logic in PHP and calling salesforce get API with above mention SOQL query as query parameter.

marked as duplicate by Dave Humm, battery.cord, glls, Derek F, Himanshu Nov 13 '17 at 3:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • yes. You can do Id > last returned id – kurunve Nov 10 '17 at 7:07
2

If you're using the SOAP API, just use queryMore. There's no need to use LIMIT/OFFSET, as the system will keep feeding you records until the end. The basic process looks like this:

QueryResult qr = connector.query(queryString);
while(true) {
  // Process qr.records
  if(qr.done) {
    break;
  }
  qr = connector.queryMore(qr.queryLocator);
}

A more specific example is included in the documentation:

public void queryRecords() {
   QueryResult qResult = null;
   try {
      String soqlQuery = "SELECT FirstName, LastName FROM Contact";
      qResult = connection.query(soqlQuery);
      boolean done = false;
      if (qResult.getSize() > 0) {
         System.out.println("Logged-in user can see a total of "
            + qResult.getSize() + " contact records.");
         while (!done) {
            SObject[] records = qResult.getRecords();
            for (int i = 0; i < records.length; ++i) {
               Contact con = (Contact) records[i];
               String fName = con.getFirstName();
               String lName = con.getLastName();
               if (fName == null) {
                  System.out.println("Contact " + (i + 1) + ": " + lName);
               } else {
                  System.out.println("Contact " + (i + 1) + ": " + fName
                        + " " + lName);
               }
            }
            if (qResult.isDone()) {
               done = true;
            } else {
               qResult = connection.queryMore(qResult.getQueryLocator());
            }
         }
      } else {
         System.out.println("No records found.");
      }
      System.out.println("\nQuery succesfully executed.");
   } catch (ConnectionException ce) {
      ce.printStackTrace();
   }
}

Note that if you're really trying to do that sort of pagination, consider sorting by Id:

SELECT ... FROM ... WHERE Id > 'Last-Returned-Id' ORDER BY Id ASC LIMIT 2000

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