Build a Java Backend that Connects with Salesforce
Tags: java, api, salesforce, ecosystem
Published: 2022-11-02 12:50:47

### 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](https://www.google.com/url?q=https://blog.postman.com/intro-to-apis-history-of-apis&sa=D&source=docs&ust=1665513405618360&usg=AOvVaw1aHWllLUfwdN90JcxSUynU) 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](https://developer.salesforce.com/docs/apis)_ of them) include:

 



* [SOAP API](https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_quickstart_intro.htm) 
* [REST API](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_rest.htm) 
* [Bulk API](https://developer.salesforce.com/docs/atlas.en-us.224.0.api_asynch.meta/api_asynch/asynch_api_intro.htm) 
* [Metadata API](https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_intro.htm) 
* [Chatter REST API](https://developer.salesforce.com/docs/atlas.en-us.216.0.chatterapi.meta/chatterapi/intro_what_is_chatter_connect.htm) 
* [Streaming API](https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/intro_stream.htm) 
* [Tooling API](https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/intro_api_tooling.htm) 

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)](https://github.com/forcedotcom/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](https://resources.docs.salesforce.com/latest/latest/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf). 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](https://www.oracle.com/java/technologies/downloads/) 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](https://www.eclipse.org/downloads/packages/). 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](https://mvnrepository.com/artifact/com.force.api/force-wsc) for the WSC shows all of the versions available:


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a42wixm76jtohqde34ah.png)

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](https://mvnrepository.com/artifact/com.force.api/force-wsc/56.0.0).


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdaumjp0pv5sdnh22hvz.png)

Under **Files**, we click on **View All**. This takes us to the [`](https://repo1.maven.org/maven2/com/force/api/force-wsc/54.0.0/) of actual `.jar` files we will need to download.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljzxxqpcwtu0uyg3w0ut.png)

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.”


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d0w9s1fw4krlu6zy4ic.png)

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();
    }
  }
}
```



### 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**. 


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4hhgxoz3orybvg6synx4.png)

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**.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o5l0x0a88wihc43sak17.png)

## 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**.


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3b2a1qg192578xojo6k9.png)

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


![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okfwgk2eibxsb4efzaf1.png)

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:



* [Web Services Connector (GitHub)](https://github.com/forcedotcom/wsc)
* [Salesforce Master GitHub Repo (a treasure trove of SFDC repositories)](https://github.com/forcedotcom)