DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Build Even Faster Quarkus Applications With fast-jar
  • Microsoft Teams for Developers: Enhancing Communication With Call Initiating and Recording
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Automated Application Integration With Flask, Kakfa, and API Logic Server

Trending

  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How to Create a Successful API Ecosystem
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Build a Java Backend That Connects With Salesforce

Build a Java Backend That Connects With Salesforce

A discussion on how to implement Java and SFDC applications. Let's start talking!

By 
Michael Bogan user avatar
Michael Bogan
DZone Core CORE ·
Nov. 08, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
10.4K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • SOAP API
  • REST API
  • Bulk API
  • Metadata API
  • Chatter REST API
  • Streaming API
  • Tooling API

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 its 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:

Force WSC


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.

WSC version


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

force-wsc versions

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 a 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, and API. We click on “Generate Enterprise WSDL.”

API setup


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:

Shell
 
$ 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:

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

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.

Add classpath


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.

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!

Sales Leads in our Salesforce org


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)
  • Salesforce Master GitHub Repo (a treasure trove of SFDC repositories)
API Customer relationship management Integrated development environment Java Development Kit SOAP Use case application Build (game engine) JAR (file format) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Build Even Faster Quarkus Applications With fast-jar
  • Microsoft Teams for Developers: Enhancing Communication With Call Initiating and Recording
  • Five Java Developer Must-Haves for Ultra-Fast Startup Solutions
  • Automated Application Integration With Flask, Kakfa, and API Logic Server

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!