Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Step-by-Step Guide To Get Started With Salesforce REST API using Java

67,783 views

Published on

To download the presentation, please navigate to URL http://www.asagarwal.com/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java

How do you connect to Salesforce from an external application, say, for example Java? If you Google for the information, you will quickly learn that Salesforce provides different methods and different APIs. But what you will probably not find is a step-by-step guide teaching you exactly how to do it. In this blog post, I have stitched together a complete step-by-step instructions with screenshots on how to download, install, configure and use Salesforce's REST API to connect from a java program and execute some basic transactions (query, insert, update, delete records). In less than 45 minutes, you will have a fully functional java program that will connect to Salesforce and execute transactions in Salesforce.

Published in: Technology
  • Login to see the comments

Step-by-Step Guide To Get Started With Salesforce REST API using Java

  1. 1. Step-By-Step Guide To Get Started with Salesforce REST API using Java Ver 1.0 Updated on 02-Jan-2015
  2. 2. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Related Blog Post 2 Step-By-Step Guide to Get Started with Salesforce REST API using Java - http://www.asagarwal.com/2401/step-by- step-guide-to-get-started-with-salesforce-rest-api-using- java Please refer to the blog post for complete article, useful information and related links
  3. 3. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Ingredients 3 You will need the following to use Salesforce SOAP API with Java ■ Salesforce Enterprise Edition or above Or ■ Salesforce Developer Edition ■ Sign up for Free at https://developer.salesforce.com/ ■ Java ■ Eclipse IDE
  4. 4. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Using This Guide 4 A couple of standards/conventions have been followed in this guide. Here is what they mean Text with Red Background Text with Yellow Background 1. Text with White background, red border and a number. In a callout format Text with Green Background Important information. Take a closer look and follow as advised. You may not be able to complete the guide successfully if you miss these instructions General explanation/information to support actions mentioned on the slide. Will assist you in understanding what is being done and why Actions that you need to follow to configure. Carry out these steps in the order of their serial number. Appears on the bottom bar of the page on right hand side. Provides information on the Hardware and Software currently being used
  5. 5. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 5 ✓ Install Java ✓ Install Eclipse ➡ Install HttpClient ■ Install JSON Framework ■ Create Connected Apps in Salesforce ■ Setup REST Environment in Java ■ Create Java Program ■ Execute Transactions using REST API IMPORTANT For the step-by-step guide on 1st two steps of installing Java & Eclipse on your PC, please refer to my blog post at URL http://www.asagarwal.com/2395/step-by- step-guide-to-install-configure-force-com-ide-on-eclipse In this presentation we will start with installation of HttpClient
  6. 6. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install HttpClient 6 1. Navigate to URL http:// hc.apache.org/downloads.cgi Google Chrome on Mac Let’s start by installing HttpClient. This allows us to access HTTP resources 2. Click on the download link of latest GA version of HttpClient
  7. 7. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install HttpClient 7 1. Select a location on your computer to save the file. For the purpose of this exercise, I have created a separate folder with name “salesforce_api_rest” Google Chrome on Mac 2. Click Save
  8. 8. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install HttpClient 8 1. Navigate to the location where you saved the HttpClient file 2. Double click on the file name Mac OS X Yosemite 10.10
  9. 9. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install HttpClient 9 1. Uncompressing will uncompress the files in a folder Mac OS X Yosemite 10.10
  10. 10. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 10 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ➡ Install JSON Framework ■ Create Connected Apps in Salesforce ■ Setup REST Environment in Java ■ Create Java Program ■ Execute Transactions using REST API
  11. 11. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install JSON Framework 11 1. Next, navigate to URL http:// mvnrepository.com/artifact/org.json/json Google Chrome on Mac Next, we will install JSON. The JSON framework facilitates generation and parsing of JSON request and response data. 2. Click on the latest version of binary jar file to download
  12. 12. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install JSON Framework 12 1. Click on Download ( JAR ) Google Chrome on Mac
  13. 13. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install JSON Framework 13Google Chrome on Mac 1. Select the where you want to save the file. I will save it in the same “salesforce_api_rest” folder 2. Click Save
  14. 14. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Install JSON Framework 14Google Chrome on Mac 1. The jar file is downloaded in the specified folder
  15. 15. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 15 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ✓ Install JSON Framework ➡ Create Connected Apps in Salesforce ■ Setup REST Environment in Java ■ Create Java Program ■ Execute Transactions using REST API
  16. 16. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 16Google Chrome on Mac Next, we will create connect apps in Salesforce. This is required for Salesforce REST APIs as it uses OAuth authentication 2. Specify your username and password and click ‘Log in to Salesforce’ 1. Open the browser window and navigate to URL https:// login.salesforce.com/
  17. 17. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 17Google Chrome on Mac 1. Click on Setup
  18. 18. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 18Google Chrome on Mac 1. In the setup menu, click on Create -> Apps
  19. 19. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 19Google Chrome on Mac 1. Scroll down the window and click ‘New’ under Connected Apps
  20. 20. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 20Google Chrome on Mac 1. Specify the name of the connect apps. The API name will auto-populate. Specify the Contact Email 2. For other customisations like Logo Image, Icon etc. I will leave the fields blank as we are justing testing REST API. In a live environment, you may want to set these values properly
  21. 21. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 21Google Chrome on Mac 1. Check box to Enable OAuth Settings 2. Specify a callback URL. Depending on which OAuth flow you use, this is typically the URL that a user’s browser is redirected to after successful authentication. For this exercise, I will set it to a dummy value of https://localhost:8443/callback 3. Select the OAuth Scopes to Full Access (full) 4. Click Save after specifying values Settings shown on this page have been configured to work with our test environment for the purpose of this exercise. In a live environment, you should be careful about these settings
  22. 22. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 22Google Chrome on Mac 1. Note the message and click continue
  23. 23. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Connected Apps in Salesforce 23Google Chrome on Mac 1. Once connected app is created, note the consumer key 2. Also click on link to reveal the secret key and note the secret key
  24. 24. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 24 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ✓ Install JSON Framework ✓ Create Connected Apps in Salesforce ➡ Setup REST Environment in Java ■ Create Java Program ■ Execute Transactions using REST API
  25. 25. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 25 1. Once connected app is created, note the consumer key Mac OS X Yosemite 10.10 1.Navigate to the directory where you installed Eclipse and double click on Eclipse file of Application Type. We will now create a java program and use REST API to connect to Salesforce and execute some transactions in Salesforce
  26. 26. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 26Mac OS X Yosemite 10.10 1. Specify the location where you want to keep all your project files (e.g. program source codes). Create a new folder if desired. (Here I am creating a new workspace folder to keep all java programs in a separate folder) 2. Click OK
  27. 27. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 27 1. In Eclipse, click on Window -> Open Perspective -> Other Eclipse Kepler on Mac
  28. 28. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 28 1. Select Java 2. Click OK Eclipse Kepler on Mac
  29. 29. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 29 1. To create a new Java project, click on File -> New -> Java Project Eclipse Kepler on Mac We are creating a Java Project and will be adding the generated JAR files to the project
  30. 30. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 30 1. Specify the Project name 2. Specify the JRE to be used 3. Click Finish Eclipse Kepler on Mac
  31. 31. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 31 1. Click on icon to open the Java Project created in previous step 2. Hover your mouse at the corner of the window and drag it to bottom right to maximise the window Eclipse Kepler on Mac
  32. 32. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 32 2. Click Properties 1. Right click on the project name Eclipse Kepler on Mac
  33. 33. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 33 1. Select Java Build Path 2. Select Libraries 3. Click Add External JARs Eclipse Kepler on Mac
  34. 34. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 34 1. Navigate to directory where httpcomponents-client-<version>- bin.tar.gz was downloaded and uncompressed. Navigate to httpcomponents-client-<version>/lib folder 2. Select the jar files httpclient-<version>, httpcore-<version>, commons-logging- <version>, commons-codec-<version> Eclipse Kepler on Mac 3. Click Open
  35. 35. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 35 1. Note that all selected jar files gets added to the library 2. Click Add External JARs Eclipse Kepler on Mac
  36. 36. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 36 2. Select the jar files json-<version> jar files 3. Click Open 1. Navigate to directory where JSON jar file was downloaded Eclipse Kepler on Mac
  37. 37. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Setup REST Environment in Java 37 1. Note that json-<version>.jar gets added to the library 2. Click OK Eclipse Kepler on Mac
  38. 38. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 38 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ✓ Install JSON Framework ✓ Create Connected Apps in Salesforce ✓ Setup REST Environment in Java ➡ Create Java Program ■ Execute Transactions using REST API
  39. 39. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Java Program 39 1. Expand the product and Right click on the src 2. Click New 3. Click Folder Eclipse Kepler on Mac
  40. 40. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Java Program 40 1. Specify the folder name 2. Click Finish Eclipse Kepler on Mac
  41. 41. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Java Program 41 1. Right click on folder name 2. Click New 3. Click Class Eclipse Kepler on Mac
  42. 42. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Create Java Program 42 1. Specify the class name 2. Click Finish Eclipse Kepler on Mac
  43. 43. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 43 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ✓ Install JSON Framework ✓ Create Connected Apps in Salesforce ✓ Setup REST Environment in Java ✓ Create Java Program ➡ Execute Transactions using REST API
  44. 44. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 44 2. Replace the value for • USERNAME with your salesforce username • PASSWORD with your salesforce password + security token • CLIENTID with the consumer key generated after creating connected apps in Salesforce in previous steps • CLIENTSECRET with consumer secret generated after creating connected apps in Salesforce in previous steps 1. Copy and paste the following code in your main class. The code is available on the blog post 3. Click Save Eclipse Kepler on Mac This is just a trial code to ensure that we are able to connect o Salesforce using REST APIs
  45. 45. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 45 1. Right click on the class name 2. Click ‘Run As’ 3. Click ‘Java Application’ Eclipse Kepler on Mac
  46. 46. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 46 1. As you run the application, you will see a Console tab with following messages Eclipse Kepler on Mac And if you see something similar to what I see here, believe it or not, you have just successfully logged on to Salesforce using REST API
  47. 47. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 47 1.Copy and paste the full code from the blog post to Main.java file. This code will 1. Query and display 5 records from Lead object 2. Insert 1 new record in lead object with Name as ‘REST API Lead <n>’ 3. Update 1 lead records where keyword ‘-- UPDATED’ will the appended to the last name 4. Delete 1 of the updated lead record 2. Click ‘Save’ Eclipse Kepler on Mac Now let’s run a complete program that will query records and insert, update, delete records in Salesforce using REST API
  48. 48. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 48 1.Before we run the Java program, let’s logon to the Salesforce org from browser and navigate to leads tab so that we can verify the results in the browser Google Chrome on Mac 2. Navigate to Recycle Bin.
  49. 49. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 49Google Chrome on Mac 1. Note that currently Recycle Bin is empty
  50. 50. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 50 1. Moment of truth. Let’s run the application. Right click on the class name 2. Click ‘Run As’ 3. Click ‘Java Application’ Eclipse Kepler on Mac
  51. 51. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 51 1. As the java program is being executed, output will be displayed in the console window 2. Click here to maximise the console window Eclipse Kepler on Mac
  52. 52. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 52 1. Output from queryLeads() function Eclipse Kepler on Mac 2. Scroll down the page
  53. 53. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 53 1. Output from queryLeads() function 2. Output from createLeads() function 3. Output from updateLeads() function 4. Output from deleteLeads() function Eclipse Kepler on Mac
  54. 54. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Execute Transactions using REST API 54 1. Back in Salesforce browser window, refresh the recycle bin page 2. You should see 1 lead record with First Name as ‘REST API’ and Last Name as ‘Lead— UPDATED’. The Java program created 1 lead record, updated it and then deleted the record Google Chrome on Mac
  55. 55. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Steps Overview 55 ✓ Install Java ✓ Install Eclipse ✓ Install HttpClient ✓ Install JSON Framework ✓ Create Connected Apps in Salesforce ✓ Setup REST Environment in Java ✓ Create Java Program ✓ Execute Transactions using REST API
  56. 56. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce Leave Your Feedback 56 Hope you were able to connect to Salesforce using REST API from the Java program and successfully execute transactions Please do not forget to leave your comments, feedback and suggestions on my blog at URL http://www.asagarwal.com/ 2401/step-by-step-guide-to-get-started-with-salesforce-rest- api-using-java If you got stuck anywhere and were able to resolve the issue, please mention that as a comment on the blog post so that others can benefit from your experience
  57. 57. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce For Daily Dose of Useful Tips & Tricks on Salesforce.com subscribe to asagarwal.com 57
  58. 58. Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce 58 Document Change Log Version Changes 1.0 First Release

×