Salesforce REST integration JAVA Application

Force.com REST API

Salesforce REST integration JAVA Application

PROBLEM: How to connect/query/SOQL to any Salesforce.com account using REST API and JSON Payloads. This application will provide a great help in testing all kinds of Salesforce object queries in SOQLs and creating or updating various Salesforce custom as well as standard objects by the help of JSON Stings.

SOLUTION: We build a small java based application which will achieve the following objectives.

  1. User enters the Client Id, Client Secret and the file name.
  2. Connect to the Salesforce Account using the above information.
  3. After a Successful connection the User can do any of the below Operations.
  4. Enter a SOQL Query to view the Results/Working.
  5. Enter a JSON String to Create/Update/Delete a Salesforce Object (Standard/Custom).

The Solution is divided into various Blogs having all the Screenshots, Data flow diagrams, Flowcharts and Java/Html source codes. You can easily understand all the steps and can create the application on your end by the help of the various resources. The solution gives an in-depth understanding of all the steps of REST authorization from Salesforce and generation of the access Token for further REST calls. The core part of our solution is the initial authorization of the Rest Application. User can also test almost any Salesforce object query and can also transfer the Record information to be created on salesforce in the JSON String format.

 

REQUIREMENTS: Apache Server, Eclipse IDE and Salesforce Developer Edition account. The User should also have some information about Salesforce force REST cUrls or it can be learnt almost instantly while testing the Rest Application being created in this blog.

WE PROVIDE: We provide all the Screenshots, Data flow diagrams, Flowcharts and Java/Html source codes, URLs and Steps to perform. https://developer.force.com/REST

REST API1

 

STEP 1. Enable Remote Access in SFDC.

Log in to Salesforce.com with your developer account, navigate to Top Right corner, Your Name ➤ Setup ➤ Develop ➤ Remote Access, and click New to create a new remote access application if you have not already done so.

REST API2

 

If you are running the application server on your own machine (the most common case for a development environment), then the URL will look like https://localhost:8443/Rest/oauth/_callback.

After clicking Save, you will see your new application’s credentials:

q1

Click the link to reveal the consumer secret. Note – OAuth 1.0 terminology is currently used in the Remote Access screen. The OAuth 2.0 specification uses ‘client’ in place of ‘consumer’.Copy the consumer key and consumer secret, we will use them inorder to connect and receive the Authorization token.:

 

 

STEP 2. Enable SSL in Apache.

If you already have an Apache Tomcat server with SSL Enable then Jump to STEP.

 

#1. To Enable SSL we will create a keystore file to store the server’s private key and self-signed certificate by executing the following command:

Windows: %JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSARestjavaC

Unix:$JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA

and specify a password value of “changeit”.

 

#2. Uncomment the “SSL HTTP/1.1 Connector” entry in $CATALINA_BASE/conf/server.xml and modify as

<Connector executor=”tomcatThreadPool”

port=”8443″ protocol=”org.apache.coyote.http11.Http11NioProtocol”RestjavaXML

connectionTimeout=”20000″

redirectPort=”8443″ SSLEnabled=”true”

maxThreads=”150″ scheme=”https” secure=”true”  keystoreFile=”${user.home}/.keystore” keystorePass=”changeit”

clientAuth=”false” sslProtocol=”TLS”/>

 

Tomcat can use two different implementations of SSL:

  • the JSSE implementation provided as part of the Java runtime (since 1.4)
  • the APR implementation, which uses the OpenSSL engine by default.

The exact configuration details depend on which implementation is being used. The implementation used by Tomcat is chosen automatically unless it is overriden as described below. If the installation uses APR – i.e. you have installed the Tomcat native library – then it will use the APR SSL implementation, otherwise it will use the Java JSSE implementation. Make sure that you use the correct attributes for the connector you are using. The BIO and NIO connectors use JSSE whereas the APR/native connector uses APR.

 

#3. After completing these configuration changes, you must restart Tomcat as you normally do, and you should be in business. You should be able to access any web application supported by Tomcat via SSL. For example, try: https://localhost:8443

 

 

Untitled

 

 

STEP 3. Install/Run the Rest.war

 

Copy the Rest.war in the webapps folder and run the Apache Tomcat Server.

image 2

 

Open browser and Type in the URL https://localhost:8443/Rest/

Untitled 2

 

 

STEP 4. Connect/Authorize from SFDC

Select Connect/Authorize radio button on the index page and enter the information

  • File Name: This file will store the complete details of the process.
  • SFDC Client Id: The Consumer Key generated after creation of Remote Access in SFDC.
  • SFDC Client Secret: The Consumer Key generated after creation of Remote Access in SFDC.
  • Redirect URI: Locked on HTML. Force.com authenticates the user, obtains authorization for the web app to access the user’s data, if necessary, and redirects the user back to redirectUri; in this example, https://localhost:8443/Rest/oauth/_callback.
  • SFDC Enviornment: Locked on HTML.

Untitled 3

Press the Enter Button to get the below Login Page. Here the local server have redirected the request to salesforce with the above details and you need to enter your SFDC Developer account UserName and Password to Approve the REST Application.

 

Untitled 4

 

 

You will get the below Screen for Approval if the No User Approval Required field has been checked while creating the Remote Access.

 Untitled 5

 

After the Approval have been successful a new Connect.txt file will be created on C:/ containing all the information.

 

Connect.txt
CloudAnalogySFDC Temporary Code Before Redirect : aPrxaSyVmC8fBbeFjFjnFYliGc7VYaeRt9OjLvU0YLAT.QJHeXiOWcFONlN3y1O4fB1K9rTLnw==After grant_type : authorization_code is requested :______________________________SFDC Client Id : 3MVG9Y6d_dsgsdfgsdfgsdfgsdfgsdfg.L0ayBXF1c.XyhHnNVgb_SFDC Client Secret : 5848734343434493341SFDC accessToken : 00D90000000aAZE!AASDFASPS9Xeg9fA4TaADoPJtAQvZmAKAxEHqBEEZTGEJJevrwZAV0CjC2bDAuYyGTIC6C

SFDC refresh_token : 5Aep8617VFpoP.M.4voIASDFAUcttoMQdNjknZpj28QerC91.Lwn_iu6fedT_D_wkXQg==

OUTPUT:

{

“access_token”: “00D90000000aAZEASDF0jJ90cFCupu17nXa80Lbpys52UoPJtAQvZmAKAxEHqBEEZTGEJJevrwZAV0CjC2bDAuYyGTIC6C”,

“id”: “https://login.salesforce.com/id/00D90000000aAZEEA2/00590000000pGULAA2”,

“instance_url”: “https://ap1.salesforce.com”,

“issued_at”: “1327058808849”,

“refresh_token”: “5Aep8617VFpoP.M.4voIkYooa9hASDFnZpj28QerC91.Lwn_iu6fedT_D_wkXQg==”,

“signature”: “xQa5Jh2raNkqASDFAf0Uxx73ULUIYqe/iy/9WJzD4=”

}

 

 

 

image 3

22

 

 

 

STEP 5. Execute REST SOQL from SFDC

Select Connect/Authorize radio button on the index page and enter the information

  • File Name: This file will store the complete details of the process.
  • URL String: This will contain the Salesforce access URL. Example “/services/data/v20.0/sobjects/Account/describe”-> To get the Metadata of Account Object, “/services/data/v20.0/query” to fetch a Query String.

SQL String this will contain a correct SOQL Statement. Untitled 6

 

After filling all the information press Submit Query. We have used a simple Select Query to display 10 records of Account Object. The Browser will get the following Output.

 

Untitled 7

 

Query Text

 https://ap1.salesforce.com/services/data/v20.0/querySOQL : Select Name, Id from Account LIMIT 10OUTPUT:{ “done”: true, “records”: [ { “Id”: “00190000009Ev8RAAS”, “Name”: “Pandav”, “attributes”: { “type”: “Account”, “url”: “/services/data/v20.0/sobjects/Account/00190000009Ev8RAAS” } }, { “Id”: “00190000009zCSIAA2”, “Name”: “Golden Straw456”, “attributes”: { “type”: “Account”, “url”: “/services/data/v20.0/sobjects/Account/00190000009zCSIAA2” } }, { “Id”: “00190000009zCRxAAM”, “Name”: “Ajay Test Eclipse”, “attributes”: { “type”: “Account”, “url”: “/services/data/v20.0/sobjects/Account/00190000009zCRxAAM” } }, { “Id”: “00190000009zCSIAA2”, “Name”: “Golden Straw456”, “attributes”: { “type”: “Account”, “url”: “/services/data/v20.0/sobjects/Account/00190000009zCSIAA2” } }, ], “totalSize”: 10 } 
 

 

 

33

 

 

STEP 6. Create SFDC Object

Select Create SFDC Object radio button on the index page and enter the information

  • File Name: This file will store the complete details of the process.
  • URL String: This will contain the Salesforce access URL. Example “/services/data/v20.0/sobjects/Account “-> To create, update, delete the records of Account Object,
  • JSON String this will contain a the data as shown in the screenshot. Please make sure the date format is in ISO-8601 (example: 2005-08-15T15:52:01+0000) and other fields are in correct format.

 

Untitled 8

 

After filling all the information press Submit Query. We have used a simple JSON String to create an Account on Salesforce. The Browser will get the following Output.

Untitled 9

 

 

 

Create.txt
CloudAnalogyURL : https://ap1.salesforce.com/services/data/v20.0/sobjects/AccountJSON STRING :                                     {“Name” : “RESTcallCloudAnalogy”,”AccountNumber” : “2332233”,”Description” : “Newly created Account from REST call”,”NumberOfEmployees”  :34  ,”AjayCloud__SLAExpirationDate__c” : “2012-01-11T22:51:38.000+0000”

}

 

OUTPUT:

{

“errors”: [],

“id”: “0019000000A1It7AAF”,

“success”: true

}

 

 

33

 

 

 

STEP 7. Conclusion

We can test several more JSON Strings and Queries using this application. This Application is also helps in understanding the complete flow of Force.com REST API to get the Authorization from salesforce account and setting up of a SSL Apache Server.

Author: AJ

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *

× How can I help you?