step-by-step guide to get started with salesforce rest api using java v1.0

58
Step-By-Step Guide To Get Started with Salesforce REST API using Java Ver 1.0 Updated on 02-Jan-2015

Upload: ashish-agarwal

Post on 17-Jul-2015

3.193 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

Step-By-Step Guide

To Get Started with Salesforce REST API using Java

Ver 1.0 Updated on 02-Jan-2015

Page 2: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 3: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 4: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 5: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 6: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 7: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 8: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 9: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 10: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 11: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 12: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 13: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 14: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 15: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 16: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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/

Page 17: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 18: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 19: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 20: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 21: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 22: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 23: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 24: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 25: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 26: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 27: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 28: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 29: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 30: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 31: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 32: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 33: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 34: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 35: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 36: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 37: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 38: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 39: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 40: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 41: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 42: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 43: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 44: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 45: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 46: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 47: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 48: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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.

Page 49: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 50: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 51: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 52: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 53: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 54: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 55: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 56: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 57: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

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

Page 58: Step-by-Step Guide To Get Started With Salesforce REST API using Java v1.0

Visit asagarwal.com for daily dose of useful tips & tricks on Salesforce 58

Document Change LogVersion Changes

1.0 First Release