Preface – This post is part of “Salesforce Integration with SOAP API” series.

In my previous post, I have explained how to query Account record. The post is @

Salesforce Integration with SOAP API – Part 5 – Query Account Record with Java program

But say for example, if you have huge amount of record you need to fetch from Salesforce using SOAP API, then what you should do? Basically how you can bulkify your code?

Below is what Salesforce documented –

Please have a look what Salesforce tells about QueryLocator. Before going to the code, you need to understand the importance of QueryLocator.

Note: I have written a helper class which I am using to connect to Salesforce using Enterprise WSDL. To know more about that, please refer my previous post @ 

Now below is the Java code –

import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;

public class QueryAllAccounts
private EnterpriseConnection connection;

private QueryAllAccounts() {

public static void main(String[] args) {
QueryAllAccounts queryAllAccounts = new QueryAllAccounts();

private void doQuery(){
QueryResult queryResult = new QueryResult();
queryResult = getConnection().query("SELECT Id,Name,Phone,Rating FROM Account");

boolean done = false;
if(queryResult.getSize() > 0){
System.out.println("Found " + queryResult.getSize() + " records.");
for(SObject sObject : queryResult.getRecords()){
Account anAccount = (Account)sObject;
System.out.println("Account ID: " + anAccount.getId() + " Account Name: " + anAccount.getName() + " Phone: " + anAccount.getPhone() + " Rating: " + anAccount.getRating());
done = true;
queryResult = getConnection().queryMore(queryResult.getQueryLocator());
System.out.println("No record found");
}catch(ConnectionException e){

public EnterpriseConnection getConnection() {
return connection;

public void setConnection(EnterpriseConnection connection) {
this.connection = connection;


I have marked the important line in the above code snippet. Hope this post and code snippet will help you to understand the concept. Please provide your feedback.

In my next post, I will explain how you can create records using SOAP API. Till then enjoy learning Salesforce.