Wednesday, May 12, 2010

SFDC Learning Series - Part 1

Welcome to my blogs on SFDC learning series. This is my learning experience with exploring SalesForce.com programming API. SFDC is providing a very elaborative and descriptive content for developing application using their API, but in my experience finding information from SFDC Developer WebSite is not straight forward. I have struggled to get information from the web site, did lot of searching and clicked on so many links in the web site to find out the information I am looking for. So I thought it will be useful to give information for people who will be looking for exploring SFDC API from their developer website.

Introduction
SFDC provides a Cloud platform for the developers to develop, deploy and test their API. You can develop SFDC solutions using Apex, Web Services, Bulk API and Visualforce interfaces. I have started with my learing using Web Services API. This part covers my experiments with Web Services API.

Registering with Developer Community
You need to register http://developer.force.com to start using the SFDC API. Developer registration is free and after registration you can start using the API for building solutions.

Obtaining a WSDL file
To start with Web Services, we need a wsdl file. To download the wsdl file, login to http://developer.force.com. After successful login, click on the "Setup" link that can be located on the top middle section of the page. Now from the left hand side options click on App Setup --> Develop --> API. You will see a number of WSDL options for integration. I have started with enterprise WSDL.
Click on Generate Enterprise WSDL link. In the next screen, you can see the packages avaiable to your account. In my case, I am able to see the package name as "Volunteerforce". I have clicked on the "Generate" button and the browser displayed the WSDL file. I have saved it locally to my disk.

Creating Web Service Client
I have used IBM RAD 6.0 to genereate a Web Service Client from the wsdl just downloaded. You can use any IDE or Apache Axis to generate WS Clients. The Web Service client code was generated without any errors and I was eager to try the API. I tried with the login() method. first attempt I got a non-trusted channel error. I know this happened because the web service URL is SSL and I needed the client certificate to be imported to my local JVM.

Obtaining a Client Certificate.
I have logged in again to the developer community site and navigated to "App Setup --> Develop --> API" section. I found an option "Client Certificate" link to generate the certificate. I have downloaded the certificate using this link, but when I examine the certificate, I found the intermediate certificate is expired already in 2004.

So instead of using that certificate, I have downloaded it directly from https://www.salesforce.com. To download the certifcate, Open IE browser and browse the URL http://www.salesforce.com/ . Now from the page, you can click on the Security icon that will be visible in the bottom right corner of the IE status window. This will open up the Certificate wizard from there you can copy the certificate to your local disk.

After obtaining the certificate use java keytool command to import the certificate to your JVM keystore. Once this is done we are ready to test web services. I have run the program again, now I got a different error. The error was very fancy error and I did not understand why it failed. I did some googling again and found that to use the Web Services API, I need to pass a security token along with the password.

Generating the Security Token
To generate the security token, login to developer.force.com, click on "Setup" link. From the middle pane, under "My Personal Information", you can locate the link "Reset your security token", click on it. From the next screen click on the "Reset Security Token" button. This will generate the security token and email to you. Open your email account and grab the security token. Now to pass the security token you need to combine your password with security token. suppose that your password is "mypassword" and security token is "XXXXXXXXXXXX", then you need to combine it as "mypasswordXXXXXXXXXXXX".

I have tried the login() API with my email address as username and the above combination as the password. This time the API worked and I got my first program working.

Running other Samples
Thrilled with my success on the login() method, I have tried the describeGlobal() API from the Web Service. But it again failed miserably and the error message did not reveal why it failed. I did google search again and found that I need to first login using the API and then remember the sessionId created and then pass this ID to subsequent webservice calls. Now this is appearing as a difficult issue because the webservice client Proxy API is not having an option to set the sessionId. I found a SessionHeader class being generated by the webservice client, but still to figure out how to pass the sessionId to subsequent method calls.

Wait for my Part 2 series for how to resolve the sessionId issue.

No comments: