Force.com data can be accessed through SOAP and REST endpoints. While REST endpoints can be accessed directly using command line, in real life you will be accessing these endpoints programmatically using a language. Since Java is one of the most common languages used for integration with Salesforce, we’ll explore how to use Java to access data from Force.com APIs.
In this article, we will use a Scala library for Force.com which is already available on Maven Central: force-scala-lib_2.11
We are using password-based OAuth to get the Access token, and so need to get a few credentials before we get started.
Part 1: Set Up the Application
Start by Creating a connected App, and then use the credentials to populate a file (application.conf), and from there, the samples will pick up the credentials.
- Create a Connected App and get
Client ID
andClient Secret.
- Use the
username
andpassword
from the developer account created earlier. Please note password issecruity_token
+password
- Create the appropriate Structure of the maven based project as shown below
- Populate
application.conf
with the appropriate values. This file undersrc/main/resources
is where the scala library will pickup the values for connecting to Force.com Endpoint. - Add appropriate dependencies in the
pom.xml
Dependencies are listed below:
- Initialize the
Util
class
Util util = new Util(); - Instantiate the
SObject
class
SObject sObject = new SObject(“Account”,util); - Execute Appropriate CRUD Method on
SObject
class
- Get SObject List
- Create SObject (Account in this case)
- Delete sObject
- Run SOQL Query
Part 2: Running the Application
Once you have implemented the appropriate example, you can use Maven to compile and run the application. Let’s look at the Execute SOQL Example in the class com.mysalesforce.QuerySObject
:
Output will be similar to the listing below:
Summary
In this blog-post, we learned how easy it is to access Force.com REST APIs using Java. Our life became a lot easier by using a pre-built Scala library, so that we don’t have to worry about making low-level Http calls to the actual URLs exposed by Force.com platform.
You can access the full tutorial and details steps at http://clouddatafacts.com/force.com/force-rest-java.html. Full method signatures for org.salesforce.SObject and org.salesforce.Util are available there as well.
About the Author
Rajdeep Dua is Director Developer Relations at Salesforce. He is passionate about helping developers learn about Cloud Computing and Salesforce. He has over 16 years of experience in Software Product Development and Developer Relations.