Build a Java Backend that Connects to Salesforce

Part One: Java Calling Salesforce

The Salesforce ecosystem is huge. 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 old And this new, We will integrate Java-based applications with Salesforce, and we will do so through web services.

Java is a programming language and computing platform first released by Sun Microsystems in 1995. Since then, we have seen the rise of Web 2.0 and web programming languages, containers, microservices, and cloud-native architectures. Nevertheless, many new products and digital services designed for the future still 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 each other, it should come as no surprise that Java applications and Salesforce Cloud need to be differentiated. the wanted. So how do we get our Java and Salesforce.com (SFDC) applications to integrate and talk with each other?

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

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

Salesforce and the API-First Approach

If you are familiar with the SFDC ecosystem, you would 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 the Web API! While the platform has many great built-in capabilities, Salesforce wants to enable its customers to build the capabilities and custom experiences they need for their own platform. By offering APIs, Salesforce ensures that SFDCs can be customized and linked to any external application that can interact with web services. Some of Salesforce’s APIs are (and are very Among them) include:

Suffice it to say that Salesforce has a very well-covered platform by APIs, which opens 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 its sales organization. That business wants to have a Java application to push qualified leads directly into their Salesforce CRM, avoiding the need to manually enter lead data into Salesforce.

To integrate Java and Salesforce, we will be using the Salesforce SOAP API and the Web Services Connector (WSC) library, which acts as a wrapper layer that makes it easier to work with APIs in Java.

initial arrangement

The initial setup of our Java developer environment requires a number of steps. Luckily, we got guidance from this Salesforce tipsheet. However, we will provide an overview of the major steps here.

Install Java Developer Kit (JDK)

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

install eclipse

Next, you’ll need the Java Development IDE. While there are many options available to you, the steps in 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 wsc

Since we will be using Salesforce SOAP API, we need to install WSC.

Download WSC Pre-built.jar Files

The Maven repository for WSC shows all the versions available:

Navigate to the page for the version that Matches the API version of Salesforce which you are using. In our example, we will use 56.0.

WSC Version

below filesclick on us look at all, it takes us to the real .jar Files we will need to download.

force-wsc version

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 WSDL from our Salesforce org.

Generate and download WSDL for your Salesforce organization

Next, we will generate the WSDL to use in creating our .jar stub files. We cannot use pre-built .jar files here because WSDL is specific to our Salesforce organization. If we have custom objects and fields defined in our org, the WSDL will reflect them, and the generated .jar will include them.

We log in to our Salesforce developer organization. Then, we navigate establish, platform equipment, integrationAnd API, We “Generate Enterprise WSDL” Click on

API setup

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

generate java stub file

To use SOAP API with Java, we need to generate .jar stub files for use in our application project. We run the following command to generate a 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, Mainusing 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 add the four .jar files we’ve built for our project to the build path. Right-click on your project and select create path, configure build path,

Build Path Configure Build Path

In the next modal, navigate java build path In the panel on the left. click on libraries tab. to select modulepathand then click add external jar switch. The four .jar files you downloaded from the Maven repository and the generated stub file (sfdc_stub.jar), and add them. Then, click on apply and close,

add classpath

test the application

We are all set, our code and our .jar files are all over the place. Now, right-click on your Main.java file in eclipse. to select run as , java application,

Run As -> Java Application”/></p>
<p>After we run our application, we call Sales.  navigate to <strong>clue</strong> in our Salesforce organization, and we see we have a new lead!</p>
<p><img decoding=

Our Java application has successfully tapped into our Salesforce organization via SOAP API using WSC to insert a new lead!

next steps

Now that we’ve verified our initial proof of concept, you can start expanding your Java application to perform other operations, including reads, updates, and deletions. You can include your own custom business logic to build a Java application that handles your Salesforce organization data for you!

For more information on several of the main concepts we cover, here are links to helpful resources:

Leave a Comment