DEV Community

Cover image for Build a Java Backend that Connects with Salesforce
Michael Bogan for Salesforce Developers

Posted on

Build a Java Backend that Connects with Salesforce

Part One: Java Calling Salesforce

The Salesforce ecosystem is pretty massive. Developers need to work hard to stay up-to-date with the latest and greatest features and tools coming from Salesforce. In this article, we’ll mix a little bit of the old and the new. We’ll combine Java-based applications with Salesforce, and we’ll do so via web services.

Java is a programming language and computing platform first released by Sun Microsystems back in 1995. Since then, we’ve seen the rise of Web 2.0 and web programming languages, containers, microservices, and cloud-native architectures. Yet even now, many new products and digital services designed for the future continue to rely on Java. Java will be around for the foreseeable future.

Meanwhile, Salesforce has established itself as the world’s top CRM and customer success platform. With today’s enterprises relying on multi- and hybrid-cloud setups with applications that need to integrate seamlessly with one another, it’s not surprising that Java applications and the Salesforce Cloud need to intersect. So how do we get our Java and Salesforce.com (SFDC) applications integrated and talking with one another?

In this article, we’ll discuss how to implement this integration using the Web Services Connector (WSC). We’ll use an example use case—a business that wants to use its Java application to manipulate Salesforce org data—and show you how to set up your developer environment and connect the pieces.

However, before we start, let’s talk briefly about Salesforce and its API-first approach.

Salesforce and the API-first Approach

If you’re familiar with the SFDC ecosystem, then you will know that Salesforce takes an API-first approach when building features on its platform. In fact, Salesforce was one of the first companies to deploy web APIs! While the platform has many great built-in capabilities, Salesforce wants to enable its customers to create the capabilities and custom experiences they need for their own platforms. By offering APIs, Salesforce ensures that SFDC can be customized and connected to any external application that can interact with web services. Some of the APIs from Salesforce (and there are a lot of them) include:

Suffice it to say that Salesforce has its platform pretty well-covered by APIs, opening up access to its capabilities for countless different use cases. That being said, let’s dive into our use case.

In our example use case, we have a Java application that helps a business generate leads for their sales organization. That business would like the Java application to push the qualified leads directly to their Salesforce CRM, avoiding the need for manually entering lead data into Salesforce.

To integrate Java and Salesforce, we’ll use the Salesforce SOAP API and the Web Services Connector (WSC) library, which serves as a wrapper layer that simplifies working with the API in Java.

Initial Setup

The initial setup of our Java developer environment requires taking several steps. Fortunately, we had guidance from this Salesforce tipsheet. Nonetheless, we’ll provide an overview of the major steps here.

Install the Java Developer Kit (JDK)

To use Salesforce APIs in our Java application, we’ll install the Java Developer Kit (JDK) at version 11.0 or later. You can do that by visiting this page and finding the appropriate binary for your local machine.

Install Eclipse

Next, you’ll need a Java development IDE. While there are several options available to you, the steps for our walk-through will go with the Eclipse IDE. You can find the latest Eclipse IDE packages here. Download and install the IDE on your local machine.

Install the WSC

Since we’ll be using the Salesforce SOAP API, we need to install the WSC.

Download WSC pre-built .jar files

The Maven repository for the WSC shows all of the versions available:

Navigate to the page for the version that matches the API version of Salesforce that you’re using. In our example, we’ll use 56.0.0.

Under Files, we click on View All. This takes us to the ` of actual .jar files we will need to download.

Download the following four files to your local machine:

  1. force-wsc-56.0.0-javadoc.jar
  2. force-wsc-56.0.0-sources.jar
  3. force-wsc-56.0.0-uber.jar
  4. force-wsc-56.0.0.jar

We will use these files to generate stub files with the WSDLs from our Salesforce org.

Generate and download the WSDL for your Salesforce org

Next, we’ll generate an WSDL to use in generating our .jar stub files. We can’t use pre-built .jar files here because the WSDL is specific to our Salesforce Org. If there are custom objects and fields defined in our Org, then the WSDL will reflect those, and the generated .jar will contain them.

We log into our Salesforce developer organization. Then, we navigate to Setup, Platform Tools, Integrations, API. We click on “Generate Enterprise WSDL.”

This will open a new tab in your browser, displaying your WSDL file. Save this file to your local machine with the name sfdc.wsdl. Put it in the same folder where you downloaded the WSC .jar files.

Generate Java stub file

To use the SOAP API with Java, we need to generate .jar stub files for use in our application project. We run the following command to generate the stub file:

`
$ java -classpath force-wsc-56.0.0-uber.jar com.sforce.ws.tools.wsdlc sfdc.wsdl sfdc_stub.jar

[WSC][wsdlc.main:72]Generating Java files from schema ...
[WSC][wsdlc.main:72]Generating 1206 java files.
[WSC][wsdlc.main:72]Compiled 1210 java files.
[WSC][wsdlc.main:72]Generating jar file ... sfdc_stub.jar
[WSC][wsdlc.main:72]Generated jar file sfdc_stub.jar
`

Create Java Project in Eclipse

In Eclipse, start a new Java project and a new Java class, Main, using the following code:

`
package wsc;
// Depending on wsdl chosen, change this to enterprise or partner
import com.sforce.soap.enterprise.;
import com.sforce.soap.enterprise.sobject.
;

import com.sforce.ws.*;

public class Main {
public static void main(String[] args) {
ConnectorConfig credentials = new ConnectorConfig();
credentials.setPassword("YourPassword!apPenDdedSecurityToken);
credentials.setUsername("yoursalesforceusername@yourdomain.com");

EnterpriseConnection connection;
try {
  connection = Connector.newConnection(credentials);
  Lead le = new Lead();
  le.setLastName("AcmeEnterprises");
  le.setCompany("JavaPush");
  le.setIndustry("Hospitality"); 
  connection.create(new Lead[]{ le });
} catch (ConnectionException e) {
  e.printStackTrace();
}
Enter fullscreen mode Exit fullscreen mode

}
}
`

Add .jar files to project

Next, we need to connect the generated four .jar files with the build path for our project. Right-click on your project and select Build Path, Configure Build Path.

In the modal that comes up, navigate to Java Build Path in the left side panel. Click on the Libraries tab. Select Modulepath, and then click the Add External JARs button. Find the four .jar files that you downloaded from the Maven repository and the generated stub file (sfdc_stub.jar), and add them. Then, click on Apply and Close.

Test the Application

We are all set up, with our code and our .jar files all in place. Now, right-click on your Main.java file in Eclipse. Select Run As -> Java Application.

After we run our application, we navigate to Sales Leads in our Salesforce org, and we see that we have a new lead!

Our Java application has successfully tapped into our Salesforce org through the SOAP API, using the WSC to insert a new lead!

Next Steps

Now that we have verified the initial proof of concept, you can begin expanding your Java application to perform other operations, including reads, updates, and deletions. You can incorporate your own custom business logic to create a Java application that handles your Salesforce org data for you!

For more information on many of the core concepts we covered, here are links to helpful resources:

Top comments (0)