Force.com REST APIs with Java Using a Library Based on Scala
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 and
- Use the
password from the developer account created earlier. Please note password is
- Create the appropriate Structure of the maven based project as shown below
| |-- main
| | |-- java
| | | `-- com
| | | `-- mysalesforce
| | |
| | `-- resources
| | |-- application.conf
| | `-- application.conf.sample
application.conf with the appropriate values. This file under
src/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:
<!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<!-- http://mvnrepository.com/artifact/com.google.code.gson/gson -->
<!-- http://mvnrepository.com/artifact/org.salesforce/force-scala-lib_2.11 -->
<!-- http://mvnrepository.com/artifact/com.typesafe.play/play-json_2.11 -->
- Initialize the
Util util = new Util();
- Instantiate the
SObject sObject = new SObject(“Account”,util);
- Execute Appropriate CRUD Method on
- Get SObject List
String body = sObject.getList();
- Create SObject (Account in this case)
SObject sObject = new SObject("Account",util);
- Delete sObject
- Run SOQL Query
String soql = "SELECT+name+from+Account";
String response = sObject.executeSOQL(soql);
JSONObject obj = new JSONObject(response);
JSONArray array = obj.getJSONArray("records");
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
$ mvn compile
$ mvn exec:java -Dexec.mainClass="com.mysalesforce.QuerySObject"
Output will be similar to the listing below:
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.