28
By Forcetree.com Writing a JAVA program to connect to SFDC Copyright Forcetree.com

Forcetree.com writing a java program to connect to sfdc

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

By Forcetree.com

Writing a JAVA program to connect to SFDC

Page 2: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

It is assumed that you have very little programming knowledge in JAVA and APEX for salesforce.com, or the other SOFTWARES used in the process.

This guide is intended to give you a step by step procedure to write a simple JAVA program and connect to Salesforce.com.

This guide should approximately take 1 to 2 hours or less to complete.

Abstract:

Page 3: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 1: Installing JDK

Page 4: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Page 5: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Page 6: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

STEP 2: - VideoSetting the CLASSPATH: Follow the instructions as shown in the video.Source: http://www.youtube.com/watch?v=j5WJjknVrPsGo to the next slide to play the video

Page 7: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

STEP 3: Installing Eclipse - Videosource: http://www.youtube.com/watch?v=FZXUgXtjlEEGo to the next slide to play the video

Page 8: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Install the Force.com plugin for ECLIPSE (optional)

Even though it’s optional, it’s highly recommended that you do this step. Your future developments would be dependent on this plugin

http://wiki.developerforce.com/page/Force.com_IDE_Installation_for_Eclipse_3.3.x

STEP 4:

Page 9: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Follow the instructions as listed in the below URL

http://bogeyman2007.blogspot.com/2009/02/installing-apache-tomcat-on-linux-and.html

STEP 5: Installing TOMCAT

Page 10: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Courtesy : http://bogeyman2007.blogspot.com/2009/03/installing-apache-axis.html

Download the latest version of Apache Axis from ws.apache.org/axis.

STEP 6: Installing Apache Axis

Page 11: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Page 12: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Page 13: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Download the ZIP fileExtract the ZIP file and save as C:\axis-1_4Download the following files Activation.jarhttp://www.java2s.com/Code/JarDownload/activation.jar.zip Mail.jarhttp://www.java2s.com/Code/JarDownload/mail.jar.zip Xmlsec.jarhttp://www.java2s.com/Code/JarDownload/xmlsec.jar.zip Extract these 3 supporting libraries and copy

the .jar files to the lib folder inside the WEB-INF folder of your Axis installation folder. (image shown in next slide)

Page 14: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Page 15: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Copy “activation.jar” and “mail.jar” and paste it into the location shown below. Also, verify that the contents of the location shown below is the same for you.

Page 16: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Copy the axis folder inside the webapps folder of the Axis distribution folder, to your Apache Tomcat webapps folder. (as in the images shown below)

Page 17: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Paste the copied folder into the “webapps” folder as shown below

Page 18: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Set the following path variables. Refer Slide 6 if you are unsure how to set path variables.

Setting the PATH variables

Page 19: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

AXIS_HOME = C:\axis-1_4AXIS_LIB = %AXIS_HOME%\libAXISCLASSPATH = %AXIS_LIB%\axis.jar;%AXIS_LIB

%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar

CLASSPATH = C:\Program Files\Java\jdk1.7.0_02\bin;C:\Program Files\Java\jdk1.7.0_02\jre\bin;C:\axis-1_4;C:\axis-1_4\lib\wsdl4j-1.5.1.jar;C:\axis-1_4\lib\mail.jar;C:\axis-1_4\lib\activation.jar;C:\axis-1_4\lib\saaj.jar;C:\axis-1_4\lib\log4j-1.2.8.jar;C:\axis-1_4\lib\jaxrpc.jar;C:\axis-1_4\lib\commons-logging-1.0.4.jar;C:\axis-1_4\lib\commons-discovery-0.2.jar;C:\axis-1_4\lib\axis.jar;C:\axis-1_4\lib\axis-ant.jar

Page 20: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Start your Apache Tomcat or restart it if you've got it running.

Access http://localhost:8080/axis. If you see the result like below this, it means you have installed Apache Axis successfully. You can also click the Validation link to check

if your Axis' installation configuration is fine. 

Page 21: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

STEP 7: Getting to the Salesforce PartThe steps in this video are explained in the slides below.Youtube URL: http://www.youtube.com/watch?v=tK2CiLb5vgc

Go to the next slide to play the video

Page 22: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Download the “Enterprise WSDL” as shown in the video. Create a folder on your desktop called “demo2”. Create a

folder inside “demo2” called “Downloads”. Paste the “enterprise.wsdl.xml” file into the “Downloads” folder.

Page 23: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Go to the command prompt and execute the following command. C:\Users\436321\Desktop\demo2>java

org.apache.axis.wsdl.WSDL2Java -a c:\Users\436321\desktop\demo2\downloads\enterprise.wsdl.xml

Page 24: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

After you execute the command, a new folder called “com” would be created inside your “demo2” folder.

Start “eclipse”.Follow the steps outlined in the video in the

next slide. The JAVA program source code is also provided after the video.

Page 25: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Go to the next slide to play the video

Youtube URL: http://www.youtube.com/watch?v=gAx0XVgne3w

Page 26: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

import com.sforce.soap.enterprise.LoginResult; import com.sforce.soap.enterprise.QueryResult; import com.sforce.soap.enterprise.SessionHeader; import com.sforce.soap.enterprise.SforceServiceLocator; import com.sforce.soap.enterprise.SoapBindingStub; import com.sforce.soap.enterprise.sobject.Contact;

public class RunDemo { private static final String USERNAME = “provide username here"; private static final String TOKEN = “provide token here"; private static final String PASSWORD = “provide password here" + TOKEN; private SoapBindingStub binding; public static void main(String[] args) { RunDemo d = new RunDemo(); try { d.login(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { d.search(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }

Page 27: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

private void login() throws Exception{ binding = (SoapBindingStub) new SforceServiceLocator().getSoap(); LoginResult loginResult = binding.login(USERNAME, PASSWORD);

binding._setProperty(SoapBindingStub.ENDPOINT_ADDRESS_PROPERTY, loginResult.getServerUrl());

SessionHeader sh = new SessionHeader(); sh.setSessionId(loginResult.getSessionId()); binding.setHeader(new

SforceServiceLocator().getServiceName().getNamespaceURI(),"SessionHeader",sh);

} private void search() throws Exception{ QueryResult qr=binding.query("Select FirstName, LastName from Contact"); if (qr.getSize() > 0) for (int i=0; i<qr.getSize(); i++){ Contact c = (Contact) qr.getRecords(i); System.err.println(c.getFirstName() +" "+ c.getLastName() );

}

}

}

Page 28: Forcetree.com writing  a java program to connect  to sfdc

Copyright Forcetree.com

Thank You

Like us at: Forcetree.com - Facebook pageVisit us at: Forcetree.com