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.

  1. Create a Connected App and get Client ID and Client Secret.
  2. Use the username and password from the developer account created earlier. Please note password is secruity_token + password
  3. Create the appropriate Structure of the maven based project as shown below
    |-- pom.xml
    |-- src
    |   |-- main
    |   |   |-- java
    |   |   |   `-- com
    |   |   |       `-- mysalesforce
    |   |   |
    |   |   `-- resources
    |   |       |-- application.conf
    |   |       `-- application.conf.sample
    
  4. Populate 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.
  5. Add appropriate dependencies in the pom.xml Dependencies are listed below:
    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
      <!-- http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
      <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
      </dependency>
      <dependency>
          <groupId>org.skife.com.typesafe.config</groupId>
          <artifactId>typesafe-config</artifactId>
          <version>0.3.0</version>
      </dependency>
      <!-- http://mvnrepository.com/artifact/com.google.code.gson/gson -->
      <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.6.2</version>
      </dependency>
      <!-- http://mvnrepository.com/artifact/org.salesforce/force-scala-lib_2.11 -->
      <dependency>
          <groupId>org.salesforce</groupId>
          <artifactId>force-scala-lib_2.11</artifactId>
          <version>1.0.2</version>
      </dependency>
      <!-- http://mvnrepository.com/artifact/com.typesafe.play/play-json_2.11 -->
      <dependency>
          <groupId>com.typesafe.play</groupId>
          <artifactId>play-json_2.11</artifactId>
          <version>2.5.3</version>
      </dependency>
      <dependency>
          <groupId>org.json</groupId>
          <artifactId>json</artifactId>
          <version>20160212</version>
      </dependency>
    </dependencies>
  1. Initialize the Util class
    Util util = new Util();
  2. Instantiate the SObject class
    SObject sObject = new SObject(“Account”,util);
  3. Execute Appropriate CRUD Method on SObject class
    • Get SObject List
           String body = sObject.getList();
      
    • Create SObject (Account in this case)
           SObject sObject = new SObject("Account",util);
      
    • Delete sObject
           sObject.deleteSObject(objectId);
      
    • 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 com.mysalesforce.QuerySObject:

$ mvn compile
$ mvn exec:java -Dexec.mainClass="com.mysalesforce.QuerySObject"

Output will be similar to the listing below:

[
  {
      "attributes": {
          "type": "Account",
          "url": "/services/data/v35.0/sobjects/Account/00128000005hlZKAAY"
      },
      "Name": "HWAccount"
  },
  {
      "attributes": {
          "type": "Account",
          "url": "/services/data/v35.0/sobjects/Account/00128000003L9tVAAS"
      },
      "Name": "GenePoint"
  },
  ...
]

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.

Leave your comments...

Force.com REST APIs with Java Using a Library Based on Scala