Salesforce Integration

Calling Salesforce REST API web services using Java and OAuth 2.0

Salesforce allows you to write Apex class methods and expose them as a REST or SOAP web service operations. By making your methods callable through the web, any external applications can integrate with Salesforce to perform the operations. Let’s say you built a custom application on platform or want to use an existing one (whose methods are exposed) – you can access from your external apps written in various different technologies such as Java/.Net/Ruby/AngularJS. In this tutorial, I will go over how we can access Salesforce REST API web services using Java client/program.

Before we go to the Java code, we need to understand how you can create the REST API web services in Salesforce or have basic understanding about it. The best place I think to have a  look at  this Trailhead tutorial Apex REST Webservices which explains it in great details. From the tutorial – I am going to use the method of the class – ‘CaseManager’ as my service operations and ‘CaseManagerTest’ is also provided for code coverage in Salesforce. I would suggest – copy the content of the 2 classes in your Salesforce developer edition so that you can use the Java code below without any changes. Just as an FYI, CaseManager – This Apex class manages case records. It has five methods and each of them corresponds to a HTTP method as denoted by annotation’s at method level such as @HttpXxx and produces JSON response. CaseManagerTest – The test class is needed to provide the required minimum code coverage of 75%, but we should always target 100%.

So now we have the service available, next we need the Java client to call the operations – but before we need to understand how you can do so. Salesforce uses the OAuth protocol to allow users of external applications to securely access. You must authenticate the application user using OAuth 2.0. Follow this link to Set Up a Connected App as a required step. I followed the same in my development org and it looks like below –

Using OAuth – there are 3 ways to authenticate between your application and Salesforce, depending upon your requirements – however, I am going to use the Username-password based flow in this article. If you want to understand these in details – follow the links below.

In the Username-password flow, the info you need to provide are – ‘Username’, ‘Password’, ‘Consumer Key (ClientId)’ and ‘Consumer Secret (Client Secret)’. Below is the Java code which authenticates to Salesforce, successfully makes connection, gets the response in JSON format and processes it. Also, download the latest version of these 2 jars as they are required in the Java program to compile and run successfully – JSON and HTTPClient. In this class I have only implemented 2 methods, one to query the Cases (READ) and other one is to insert (CREATE) a new case. I am leaving the UPDATE, UPSERT and DELETE operations as they will follow similar approach.

Note – If you want to use the code below, make sure to remove the special characters which might appear after copying to your editor.

Feel Free to provide your valuable comments or point out any issues or scope of improvement in the above post.