Preface – This post is part of “Salesforce Integration with SOAP API” series.
In my previous post, I have explained how can you configure your Eclipse IDE so that you can start writing you own Java program for connecting to Salesforce using SOAP API and also the Java program to check the connectivity. If you are not familiar, I would request you to go through my previous posts. The links are given below –
- Salesforce Integration with SOAP API – Part 3 – Configure Eclipse for Enterprise WSDL
- Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDL
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.ws.ConnectionException;
public class FindAccount {
private EnterpriseConnection connection;
private FindAccount() {
setConnection(ConnectionHelper.doConnect());
}
public static void main(String[] args) {
FindAccount findAccount = new FindAccount();
findAccount.doFind();
}
private void doFind(){
QueryResult result = new QueryResult();
try{
result = getConnection().query("SELECT Id,Name,Phone,Rating FROM Account WHERE Name = 'Burlington Textiles Corp of America'");
if(result.getSize() > 0){
System.out.println("Found " + result.getSize() + " records.");
Account anAccount = (Account)result.getRecords()[0];
System.out.println("ID: " + anAccount.getId() + " Name: " + anAccount.getName() + " Phone: " + anAccount.getPhone() + " Rating: " + anAccount.getRating());
}
}catch(ConnectionException e){
e.printStackTrace();
}
}
public EnterpriseConnection getConnection() {
return connection;
}
public void setConnection(EnterpriseConnection connection) {
this.connection = connection;
}
}
I hope this post will help you to understand how you can fetch a single record using query() method. In my next post, I will explain how you can handle huge amount of data while doing the query. Till then good bye and keep learning.
Any feedback, please let me know. Thanks.