It is very common for any Salesforce developer to come across scenarios involving integration 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
1) 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.
2) 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.
3) 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.
4) 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,
package partner; | |
import com.sforce.soap.partner.Connector; | |
import com.sforce.soap.partner.DeleteResult; | |
import com.sforce.soap.partner.PartnerConnection; | |
import com.sforce.soap.partner.QueryResult; | |
import com.sforce.soap.partner.SaveResult; | |
import com.sforce.soap.partner.Error; | |
import com.sforce.soap.partner.sobject.SObject; | |
import com.sforce.ws.ConnectionException; | |
import com.sforce.ws.ConnectorConfig; | |
public class PartnerExample { | |
//Hardcoded username and password | |
static final String USERNAME = "USERNAME here"; | |
static final String PASSWORD = "Password + Security token here"; | |
static PartnerConnection connection; | |
public static void main(String[] args) { | |
//Creating connector config instance with username and password | |
ConnectorConfig config = new ConnectorConfig(); | |
config.setUsername(USERNAME); | |
config.setPassword(PASSWORD); | |
try { | |
//Passing configuration to connection object | |
connection = Connector.newConnection(config); | |
//If connection is successful session Id can be obtained with below line | |
System.out.println("SessionId: "+config.getSessionId()); | |
//Once connection is established records can be queried as below | |
QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName FROM Contact LIMIT 5"); | |
//Query results can be be convereted to sobject array using below method | |
SObject[] sObjList = queryResults.getRecords(); | |
for(SObject so:sObjList){ | |
System.out.println("Query Result"+so); | |
} | |
//New records are created in salesforce using below lines, | |
SObject[] records = new SObject[1]; | |
SObject so = new SObject(); | |
so.setType("Account"); | |
so.setField("Name", "SOAP Testing Account"); | |
records[0] = so; | |
SaveResult[] saveResults = connection.create(records); | |
System.out.println("Account Creation"+saveResults); | |
for(SaveResult sr:saveResults){ | |
System.out.println("Account Creation"+sr); | |
} | |
} 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,