Using the Force.com Web Services Connector to access the Salesforce.com API

In this post we will be creating a Salesforce.com client application that can communicate with and alter the Salesforce.com data in your org.  This would be the building blocks for a custom Java on-premise app.  We are using the WSC open source project for connection to Salesforce.com.  This is a web service client stack that makes it easier to use the Force.com API.  Find it here: http://code.google.com/p/sfdc-wsc/

Here is my configuration:

  • I am using Eclipse (Juno) on a Mac OS X (10.8.2).’
  • Java 1.6.0_37 (JDK 1.6 is required)

Steps:

  • Create a project in eclipse.  I named mine: sfdc-wsc-enterprise
  • Log into your salesforce.com org and download the necessary WSDL file (Setup –> Develop –> API –> Enterprise WSDL)
  • Save the WSDL to the top level folder of your eclipse project: enterprise.wsdl
  • Download the WSC Jar.  I downloaded wsc_22.jar and placed it the top level folder of my project.
  • Issued the following command from the command line:
~/dev/sfdc-wsc-enterprise cloudguy$ java -classpath wsc-22.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar
  • This generates the enterprise.jar file and places it in your directory path.
  • Add the JARs to your project (Project –> Properties –> Java Build Bath –> Libraries)
Java-Build-Path
  • Add a new package to your project: com.cloudpremise.samples.sfdcWscEnterprise
  • Add a new class, SimpleIntegration.class to the new project:
package com.cloudpremise.samples.sfdcWscEnterprise;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class SimpleIntegration {
static EnterpriseConnection connection;
  public static void main(String[] args) {
    ConnectorConfig conn = new ConnectorConfig();
    conn.setUsername("<your user id here>");
    conn.setPassword("<your pw + security token here>");
    try {
      connection = Connector.newConnection(conn);
      //create an account
      Account[] newAccounts = new Account[1];
      Account a = new Account();
      a.setName("New Account Name");
      newAccounts[0]=a;
      SaveResult[] saveResults = connection.create(newAccounts);
      if (saveResults[0].isSuccess()) {
      System.out.println("Created Account with ID: " + saveResults[0].getId());
      }
    } catch (ConnectionException e1) {
        e1.printStackTrace();
    }
  }
}

That’s it!  Now you can make calls directly into your Salesforce.com org from Java.  You can use the WSC connector to simplify a lot of the work for connecting into your org.  Notice no use of Axis, Jax-WS, etc.  Just a simple download of the WSC and you will be off and running in no time.

Console

new account screen shot

2 Comments on “Using the Force.com Web Services Connector to access the Salesforce.com API

  1. Hi,
    I am trying to execute your sample program to integrate java and sfdc, I am facing couple of issues like 1) where exactly i need to place the enterprise.wsdl and wsc-23-min.jar file to generate the enterprise.jar.
    Kindly let know how to resolve the issue.

    • I created a new project for this and placed the enterprise WSDL and the WSC jar in the top level folder of my project. If the blog side bar is blocking the command line here it is again for your reference:
      ~/dev/sfdc-wsc-enterprise cloudguy$ java -classpath wsc-22.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

      You can see the sfdc-wsc-enterprise is my top level folder where both the wsc jar and the enterprise.wsdl have been added.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: