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.
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;
import com.sforce.ws.ConnectionException;
public class QueryAllAccounts {
private EnterpriseConnection connection;
private QueryAllAccounts() {
setConnection(ConnectionHelper.doConnect());
}
public static void main(String[] args) {
QueryAllAccounts queryAllAccounts = new QueryAllAccounts();
queryAllAccounts.doQuery();
}
private void doQuery(){
QueryResult queryResult = new QueryResult();
try{
queryResult = getConnection().query("SELECT Id,Name,Phone,Rating FROM Account");
boolean done = false;
if(queryResult.getSize() > 0){
System.out.println("Found " + queryResult.getSize() + " records.");
while(!done){
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());
}
if(queryResult.isDone()){
done = true;
}else{
queryResult = getConnection().queryMore(queryResult.getQueryLocator());
}
}
}else{
System.out.println("No record found");
}
}catch(ConnectionException e){
e.printStackTrace();
}
}
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.