~
Integration between Java app and Salesforce – Partner WSDL

Integration between Java app and Salesforce – Partner WSDL
It is very common for any Salesforce developer to come across scenarios involving integrationsalesforcecodes logo between Salesforce and other systems. Most of these systems are based on JAVA. In this post we will cover how to integrate between Salesforce and a locally running Java application. The same logic and code can be used to connect to Salesforce from a web based java application also.
Steps to integrate
- Download Partner WSDL from your sandbox
You can find Partner WSDL in your salesforce sandbox under Setup – Develop – API. Download partner WSDL file from above location to your local system folder.
- Get WSC (Webservice connector) jar file provided by salesforce.
This jar file was initially provided by salesforce as .jar itself. Force.com webservice connector is a high performance webservice client stack implemented using a streaming parser. This gives some advantages compared to regular java classes generated from partner WSDL using other WSDL to Java tools. You can find source code to latest version of source code in force.com WSC github.
First clone the repository to your system and then build jar file using the instructions given.
- Generate Partner.jar using WSC (Webservice connector) jar generated in step 2
You can convert partner WSDL into a jar file using WSC jar file. It is single command from command line, which can be found in the above github link.
- Create project in eclipse
By the end of step 3 you have all the components needed for connecting to salesforce. Now create a new java project in eclipse. Import wsc.jar from step 2 and partner.jar from step 3 as external libraries. Then create a new class with name “PartnerExample” in the project. Then copy paste the code below:-
import com.sforce.soap.partner.Connector;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.sobject.SObject;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class PartnerExample {
static final String USERNAME = "USERNAME here";
static final String PASSWORD = "Password + Security token here";
static PartnerConnection connection;
public static void main(String[] args) {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
try {
connection = Connector.newConnection(config);
System.out.println("SessionId: "+config.getSessionId());
QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName FROM Contact LIMIT 5");
SObject[] sObjList = queryResults.getRecords();
for(SObject so:sObjList){
System.out.println("Query Result"+so);
}
SObject[] records = new SObject[1];
SObject so = new SObject();
so.setType("Account");
so.setField("Name", "SOAP Testing Account");
records[0] = so;
System.out.println("Account Creation"+connection.create(records));
} catch (ConnectionException e1) {
e1.printStackTrace();
}
}
}
It should compile without any error. Use Runas Java application option in eclipse to run the project. This code queries 5 contact and creates an account.
Reference links
There is lot of good resources demonstrating similar connectivity options between salesforce and java applications. Please check below links:-