58
Step-By-Step Guide To Get Started with Salesforce SOAP API using Java Ver 1.1 Updated on 03-Jan-2015

Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

Embed Size (px)

Citation preview

Page 1: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

Step-By-Step Guide

To Get Started with Salesforce SOAP API using Java

Ver 1.1 Updated on 03-Jan-2015

Page 2: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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 SOAP API using Java - http://www.asagarwal.com/2398/step-by-step-guide-to-get-started-with-salesforce-soap-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 SOAP API Using Java v1.1

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 SOAP API Using Java v1.1

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 SOAP API Using Java v1.1

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

Steps Overview

5

✓ Install Java ✓ Install Eclipse ➡ Install Force.com WSC (Web Services Connector) ■ Install Maven ■ Generate JAR Files ■ Download WSDL Files From Salesforce ■ Generate Java Stub File ■ Create Java Program ■ Execute Transactions using SOAP 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 Web Services Connector (WSC)

Page 6: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Force.com WSC (Web Services Connector)

6

1. Click on Applications

Mac OS X Yosemite 10.10

Let’s start by downloading WSC files from GitHub

Page 7: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Force.com WSC (Web Services Connector)

7

1. In Applications, scroll down the window and click on Utilities

Mac OS X Yosemite 10.10

Page 8: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Force.com WSC (Web Services Connector)

8

1. Click on Terminal

Mac OS X Yosemite 10.10

Page 9: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Force.com WSC (Web Services Connector)

9

1. Navigate to a directory where you want to download the WSC files.

2. Run command ‘git clone https://github.com/forcedotcom/wsc' to download the WSC files on your PC

Mac OS X Yosemite 10.10

Page 10: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

10

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ➡ Install Maven ■ Generate JAR Files ■ Download WSDL Files From Salesforce ■ Generate Java Stub File ■ Create Java Program ■ Execute Transactions using SOAP API

Page 11: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Maven

11

1. Open the browser window and navigate to URL http://maven.apache.org/

2. Click ‘Download’

Google Chrome on Mac

Next we will install Maven, which is created to compile and package

WSC files in JAR files

Page 12: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Maven

12

1. Click ‘Download’

Google Chrome on Mac

Page 13: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Maven

13

1. Select the location where you want to download the

file

2. Click Save

Google Chrome on Mac

Page 14: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Install Maven

14Mac OS X Yosemite 10.10

1. Double click on the file to uncompress the file

Page 15: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

15

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ➡ Generate JAR Files ■ Download WSDL Files From Salesforce ■ Generate Java Stub File ■ Create Java Program ■ Execute Transactions using SOAP API

Page 16: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Generate JAR Files

16Mac OS X Yosemite 10.10

1. Back in the command window, switch to directory where ‘wsc’ & ‘apache-maven’ was downloaded and uncompressed. In this example, the command that I will issue is cd /Ashish/tmp 2. Run command ls -lrt 3. Ensure that the folders ‘wsc’ & ‘apache-maven-<version>’ exist in the current directory 4. Navigate to wsc directory by type the command cd wsc 5. Run the command <path>/apache-maven-3.2.5/bin/mvn clean package -Dgpg.skip replacing the value of <path> with the actual path where you uncompressed the apache-maven file. Running this command will compile the code and package it in a jar file

1

23

45

Page 17: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Generate JAR Files

17Mac OS X Yosemite 10.10

1. “mvn clean package” command will run for a few minutes. Once it finishes. ensure that you see the message ‘BUILD SUCCESS’ 2. Run command ls -lrt 3. Ensure that a new folder with the name ‘target’ has got created 4. Run command ls -lrt target 5. Ensure that there is file ending with *uber.jar

1

2

3

4

5

Page 18: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

18

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ✓ Generate JAR Files ➡ Download WSDL Files From Salesforce ■ Generate Java Stub File ■ Create Java Program ■ Execute Transactions using SOAP API

Page 19: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

19

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/

Google Chrome on Mac

In next few steps we will download the Enterprise WSDL file from

Salesforce

Page 20: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

20

1. Click on Setup

Google Chrome on Mac

Page 21: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

21

1. Click on Develop -> API

Google Chrome on Mac

Page 22: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

22

1. Click on the WSDL that you want to work with. Here I will click on Enterprise WSDL

Google Chrome on Mac

Page 23: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

23

1. Click on Generate

Google Chrome on Mac

Page 24: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

24

1. Right click anywhere on a blank area and click on

‘Save As’

Google Chrome on Mac

Page 25: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

25

1. Select the location where you want to download the

file

2. Save file as ‘wsdl’ file

3. Click Save

Google Chrome on Mac

Page 26: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Download WSDL Files From Salesforce

26

1. If prompted, click use wsdl

Google Chrome on Mac

Page 27: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

27

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ✓ Generate JAR Files ✓ Download WSDL Files From Salesforce ➡ Generate Java Stub File ■ Create Java Program ■ Execute Transactions using SOAP API

Page 28: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Generate Java Stub File

28

1. If prompted, click use wsdl

Mac OS X Yosemite 10.10

1.Back in the terminal window, navigate to the directory where you downloaded enterprise.wsdl 2. Run command ls -lrt 3. Ensure that the file enterprise.wsdl exists in the directory 4. Run command java -classpath wsc/target/force-wsc-33.0.0-uber.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jars Ensure that you specify the correct path to force-wsc-33.0.0-uber.jar file that was generated by running the “mvn clean package” command in previous steps 5. Ensure that the command ends with Generating the jar file 6. Run command ls -lrt 7. Ensure that enterprise.jar now exists in the directory

1

23

4

5

6

7

Good job ! At the end of 7th step, we are now all set to use SOAP API to connect to Salesforce from a java program. Are

you excited? Let’s move on

Generate a JAR file from the Enterprise WSDL File

Page 29: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

29

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ✓ Generate JAR Files ✓ Download WSDL Files From Salesforce ✓ Generate Java Stub File ➡ Create Java Program ■ Execute Transactions using SOAP API

Page 30: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

30Mac 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 SOAP API to connect to Salesforce and execute some transactions in Salesforce

Page 31: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

31Mac 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 32: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

32

1. In Eclipse, click on Window -> Open Perspective -> Other

Eclipse Kepler on Mac

Page 33: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

33

1. Select Java

2. Click OK

Eclipse Kepler on Mac

Page 34: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

34

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 35: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

35

1. Specify the Project name

2. Specify the JRE to be used

3. Click Finish

Eclipse Kepler on Mac

Page 36: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

36

2. Click Properties

1. Right click on the project name

Eclipse Kepler on Mac

Page 37: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

37

1. Select Java Build Path

2. Select Libraries

3. Click Add External JARs

Eclipse Kepler on Mac

Page 38: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

38

1. Navigate to directory where

enterpise.jar was created in previous

steps

2. Double click enterprise.jar

Eclipse Kepler on Mac

Page 39: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

39

1. Note that enterprise.jar gets

added to the library2. Click Add External JARs

Eclipse Kepler on Mac

Page 40: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

40

1. Note that enterprise.jar gets

added to the library2. Click OK

1. Navigate to directory where force-wsc-<version>-uber.jar was created in previous steps

2. Double click force-wsc-<version>-uber.jar

Eclipse Kepler on Mac

Page 41: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

41

1. Note that force-wsc-<version>-uber.jar gets added to the library

2. Click OK

Eclipse Kepler on Mac

Page 42: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

42

1. Right click on the project name

2. Click New

3. Click Class

Eclipse Kepler on Mac

Page 43: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Create Java Program

43

1. Specify the class name

2. Click Finish

Eclipse Kepler on Mac

Page 44: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

44

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ✓ Generate JAR Files ✓ Download WSDL Files From Salesforce ✓ Generate Java Stub File ✓ Create Java Program ➡ Execute Transactions using SOAP API

Page 45: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

45

2. Replace the username with your salesforce username and password with your salesforce

password + security token

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 SOAP APIs

Page 46: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

46

1. Right click on the class name

2. Click ‘Run As’ 3. Click ‘Java Application’

Eclipse Kepler on Mac

Page 47: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

47

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 SOAP API. Time to

celebrate, yeah? In a while …

Page 48: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

48

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 5 new records with Name as ‘SOAP API Lead <n>

3. Update 5 lead records where keyword ‘-- UPDATED’ will the appended to the last name

4. Delete 2 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 SOAP API

Page 49: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

49

1.Before we run the Java program, let’s logon to the Salesforce org from browser and navigate to leads tab. Here I have modified the ‘All Leads’ view to show lead Name, First name, Last Name, Company, Created Date & Last Modified Date.

2.The data is sorted in the descending order of the last modified date so that we can view the newly created leads on top

Google Chrome on Mac

Page 50: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP 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 SOAP API Using Java v1.1

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

Execute Transactions using SOAP 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 SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

52

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 53: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

53

1. Switch to Salesforce Browser window and refresh the list view

2. You should see three lead records with First Name as ‘SOAP API’ and Last Name as ‘Lead <n> —

UPDATED’. The Java program created 5 lead records, updated 5 records and then deleted 2 records3. Click on Recycle Bin

Google Chrome on Mac

Page 54: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Execute Transactions using SOAP API

54

1. Two Lead records will appear in Recycle Bin as the java program deleted 2 of the 5 created records

Google Chrome on Mac

Page 55: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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

Steps Overview

55

✓ Install Java ✓ Install Eclipse ✓ Install Force.com WSC (Web Services Connector) ✓ Install Maven ✓ Generate JAR Files ✓ Download WSDL Files From Salesforce ✓ Generate Java Stub File ✓ Create Java Program ✓ Execute Transactions using SOAP API

Page 56: Step-By-Step Guide To Get Started With Salesforce SOAP API Using Java v1.1

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 SOAP 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/2398/step-by-step-guide-to-get-started-with-salesforce-soap-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 SOAP API Using Java v1.1

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 SOAP API Using Java v1.1

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

Document Change LogVersion Changes

1.0 First Release

1.1 Updated hyperlink on page 45 to correct value