Tuesday, June 4, 2013

Integrate Java App's with Salesforce.com CRM Using Force.com Custom Apex Web Service

Integrate Custom Apex WebService with Java Using Force.com Web Services Connector (WSC)

This blog post will describe that how to create an Apex web service and integrate it with Java App using Force.com Web Services Connector (WSC).

Below is an example of how to integrate Apex webservice with Force.com SOAP API  and Java Apps using Eclipse IDE.

To get started, we'll run through the following steps:

1) Create an Apex webService class

Apex Code:
/********************************************************************************/
global class SuperClass{
    global class RequestClass{
        webService String accountName;
    }
    global class ResponseClass{
        webService String responseResultID;
        webService String responseResultName;
        webService String responseResultRecordType;
    }
    webService static ResponseClass behaviourOfWebService(RequestClass reqClass){
        Account acct = new Account();
        acct.Name = reqClass.accountName;
        insert acct;
        ResponseClass resClass = new ResponseClass();
        resClass.responseResultID = acct.Id;
        resClass.responseResultName = acct.Name;
        resClass.responseResultRecordType = acct.RecordTypeId;
        return resClass;
    }
}
/********************************************************************************/

2) You need to install "Java JDK (6 or 7)" in your machine.
3) Second you need to check whether "Java JDK (6 or 7)" has installed successfully or not?

Just type in the command prompt:
java -version

You will the result same as below:
It will show you:
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-10M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

Note: If you get error then change your command prompt directory to the Java bin directory and test it again.

For Example:
C:\Program Files\Java\jdk1.6.0_45\bin> java -version

4) Download the "Enterprise WSDL" or "Partner WSDL" from your salesforce.com organization.

Log in into salesforce.com organization -> User Name ->  Setup -> App Setup section -> Develop -> API -> click on Generate Enterprise or Partner WSDL -> Save file with extension ".wsdl" as "enterprise.wsdl" or "partner.wsdl".

5) Download the Apex class "SuperClass" wsdl.

Log in into salesforce.com organization -> User Name ->  Setup -> App Setup section -> Develop -> Apex Classes-> click on SuperClass -> click Generate WSDL -> Save file with extension ".wsdl" as "mywebservice.wsdl".

6) Download the Web Services Connector (WSC) "wsc-20.jar" file from the URL "http://code.google.com/p/sfdc-wsc/downloads/list".

7) Now create JAR (Java ARchive) files for "enterprise.wsdl", "partner.wsdl" and "mywebservice.wsdl" files.

8) Copy the "tools.jar" file from the path where you have installed "Java JDK (6 or 7)".

For Example:
"C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar" and paste it in your "E:\" drive. I am doing this just for safe side so we will not have any conflicts.

Note: 
(a) "tools.jar" and "wsc-20.jar" file both must be in the same directory otherwise, you will get an error "classpath: java.io.FileNotFoundException".
(b) Just for safe side I would recommend that your "E:\" drive must have "tools.jar", "wsc-20.jar", "enterprise.wsdl", "partner.wsdl" and "mywebservice.wsdl".

9) Execute the following commands in Command Prompt to generate JAR File "JAR file (Java ARchive)" for "enterprise.wsdl" or "partner.wsdl" and "mywebservice.jar".

For Example:
java -classpath wsc-XX.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

Real Example:
Generate enterprise.jar
java -classpath E:\tools.jar;E:\wsc-20.jar com.sforce.ws.tools.wsdlc E:\enterprise.wsdl E:\enterprise.jar

Generate partner.jar
java -classpath E:\tools.jar;E:\wsc-20.jar com.sforce.ws.tools.wsdlc E:\partner.wsdl E:\partner.jar

Generate mywebservice.jar
java -classpath E:\tools.jar;E:\wsc-20.jar com.sforce.ws.tools.wsdlc E:\mywebservice.jar.wsdl E:\mywebservice.jar

Now these commands will generate "enterprise.wsdl" or "partner.wsdl" and "mywebservice.jar".

Generate Java Code in Eclipse (Creating an Enterprise WSDL Application)

Now that your environment is ready to go, it's time to build a test application to see how things are working. Most developers build client applications with the enterprise WSDL, so we’ll start with that one first.
In Eclipse, complete the following steps to build a Java application based on the enterprise WSDL.
  1. Create a new Java project named “WSC - Enterprise” (click File | New | Java Project).
  2. Add the wsc-XX.jar and enterprise.jar to the project (click Project | Properties | Java Build Path | Libraries or External Libraries, then add the JARs to the project.
  3. Add a new folder, wsc, to the src folder in your app (right-click src in Package Explorer, then click New | Folder).
  4. Create a new class src/wsc/CallWS.java and paste in the code from the code listing that follows.
  5. Replace the stub user credentials in the code with your own user name and password with security token for the appropriate static members, then save your source code.
  6. Run the application.
Your Project in Eclipse IDE will look like below screenshot:
















Java Code:
/********************************************************************************/
package wsc;

import com.sforce.soap.SuperClass.SoapConnection;
import com.sforce.soap.SuperClass.Connector;
import com.sforce.soap.SuperClass.RequestClass;
import com.sforce.soap.SuperClass.ResponseClass;

import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import com.sforce.soap.enterprise.*;


public class CallWS {


  static final String USERNAME = "Your User Name";
  static final String PASSWORD = "You User Password + Your Security Token";

  static SoapConnection MyWebserviceWSconnection;
  static EnterpriseConnection enterpriseConnection;

  public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);


    try {

      //create a connection to Enterprise API -- authentication occurs
      enterpriseConnection = com.sforce.soap.enterprise.Connector.newConnection(config);    
      // display some current settings
      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
      System.out.println("Username: "+config.getUsername());
      System.out.println("SessionId: "+config.getSessionId());


      //create new connection to exportData webservice -- no authentication information is included
      MyWebserviceWSconnection = Connector.newConnection("","");
      //include session Id (obtained from enterprise api) in exportData webservice
      MyWebserviceWSconnection.setSessionHeader(config.getSessionId());
      
      RequestClass reqClass = new RequestClass();
      reqClass.setAccountName("Account Created By Java Program");
      
      ResponseClass resClass = new ResponseClass();
      resClass = MyWebserviceWSconnection.behaviourOfWebService(reqClass);
      System.out.println("Record ID ---"+resClass.getResponseResultID());
      System.out.println("Record Name ---"+resClass.getResponseResultName());
      System.out.println("Record Record Type ---"+resClass.getResponseResultRecordType());

      //String result = MyWebserviceWSconnection.receiveData("test");
      //System.out.println("Result: "+result);


    } catch (ConnectionException e1) {
        e1.printStackTrace();
    }  
  }
}
/********************************************************************************/

Helpful Links:
http://wiki.developerforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector
http://code.google.com/p/sfdc-wsc/downloads/list

http://boards.developerforce.com/t5/Java-Development/Trying-to-call-a-simple-Apex-Web-Service-method-from-Java/td-p/206407
http://boards.developerforce.com/t5/General-Development/How-to-Call-WebService-Method-in-Java/td-p/261755
http://kperisetla.blogspot.com/2011/09/creating-custom-apex-web-service-in.html
http://forums.crmsuccess.com/t5/forums/forumtopicprintpage/board-id/JAVA_development/message-id/5930/print-single-message/false
http://boards.developerforce.com/t5/Java-Development/Problem-Calling-Apex-WebService-from-Java/td-p/206637
http://stackoverflow.com/questions/11204614/access-salesforce-apex-soap-webservice-from-java

Integrate Java App's with Salesforce.com CRM Using Force.com SOAP API & Force.com Web Services Connector (WSC)

Integrate Force.com SOAP API with Java Apps Using Force.com Web Services Connector (WSC)

Services This blog post will describe you that how to integrate Salesforce - Force.com SOAP API with Java Apps.

Below is an example of how to integrate Force.com SOAP API with Java Apps using Eclipse IDE.

Introduction to the Force.com Web Services Connector

The Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services. WSC uses a high-performing Web services client stack implemented with a streaming parser. It is the preferred tool for working with salesforce.com APIs. You can write Java applications with WSC that utilize the Force.com SOAP API, Bulk API, and Metadata API. There are even runtime libraries that let you access the Force.com SOAP API from applications running on Google App Engine.

Using WSC, you can perform operations with a few lines of code that would take many more lines of code with other Web services clients.

This article provides an introduction to WSC. The WSC can be used to invoke any doc-literal wrapped Web service, but in this article we’ll focus on the SOAP API with both the enterprise and partner WSDLs, and the Metadata API. Along the way, you’ll learn how to get started with WSC, and see an example of a console application that demonstrates WSC functionality.

Introduction to Force.com
Force.com has several API's including:
  • Force.com SOAP (Simple Object Access Protocol) API
  • Force.com Metadata API
  • Force.com Bulk API
  • Force.com Streaming API
  • Force.com REST (Representational State Transfer) API
  • Force.com Chatter API
Preparing to Integrate Java Apps with Force.com APIs:
I am assuming you have some experience building Java applications with Force.com APIs.

To get started, we'll run through the following steps:

1) First you need to install "Java JDK (6 or 7)" in your machine.
2) Second you need to check whether "Java JDK (6 or 7)" has installed successfully or not?

Just type in the command prompt:
java -version

You will the result same as below:
It will show you:
java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-10M3811)
Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

Note: If you get error then change your command prompt directory to the Java bin directory and test it again.

For Example:
C:\Program Files\Java\jdk1.6.0_45\bin> java -version

3) Download the "Enterprise WSDL" or "Partner WSDL" from your salesforce.com organization.

Log in into salesforce.com organization -> User Name ->  Setup -> App Setup section -> Develop -> API -> click on Generate Enterprise or Partner WSDL -> Save file with extension ".wsdl" as "enterprise.wsdl" or "partner.wsdl".

4) Download the Web Services Connector (WSC) "wsc-20.jar" file from the URL "http://code.google.com/p/sfdc-wsc/downloads/list".

5) Now create JAR (Java ARchive) files for "enterprise.wsdl" and "partner.wsdl" files.

6) Copy the "tools.jar" file from the path where you have installed "Java JDK (6 or 7)".

For Example:
"C:\Program Files\Java\jdk1.6.0_45\lib\tools.jar" and paste it in your "E:\" drive. I am doing this just for safe side so we don't have any conflicts.

Note: 
(a) "tools.jar" and "wsc-20.jar" file both must be in the same directory otherwise, you will get an error "classpath: java.io.FileNotFoundException".
(b) Just for safe side I would recommend that your "E:\" drive must have "tools.jar", "wsc-20.jar", "enterprise.wsdl" and "partner.wsdl".

6) Execute the following command to generate JAR File "JAR file (Java ARchive)" for "enterprise.wsdl" or "partner.wsdl".

For Example:
java -classpath wsc-XX.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

Real Example:
Generate enterprise.jar
java -classpath E:\tools.jar;E:\wsc-20.jar com.sforce.ws.tools.wsdlc E:\enterprise.wsdl E:\enterprise.jar

Generate partner.jar
java -classpath E:\tools.jar;E:\wsc-20.jar com.sforce.ws.tools.wsdlc E:\partner.wsdl E:\partner.jar

Now these commands will generate "enterprise.wsdl" or "partner.wsdl".

Generate Java Code in Eclipse (Creating an Enterprise WSDL Application)

Now that your environment is ready to go, it's time to build a test application to see how things are working. Most developers build client applications with the enterprise WSDL, so we’ll start with that one first.
In Eclipse, complete the following steps to build a Java application based on the enterprise WSDL.
  1. Create a new Java project named “WSC - Enterprise” (click File | New | Java Project).
  2. Add the wsc-XX.jar and enterprise.jar to the project (click Project | Properties | Java Build Path | Libraries or External Libraries, then add the JARs to the project.
  3. Add a new folder, wsc, to the src folder in your app (right-click src in Package Explorer, then click New | Folder).
  4. Create a new class src/wsc/Main.java and paste in the code from the code listing that follows.
  5. Replace the stub user credentials in the code with your own user name and password with security token for the appropriate static members, then save your source code.
  6. Run the application.
Your Project in Eclipse IDE will look like below screenshot:










/********************************************************************************/
package wsc;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class Main {

static final String USERNAME = "YOUR-USERNAME";
static final String PASSWORD = "YOUR-PASSWORD&SECURITY-TOKEN";
  static EnterpriseConnection connection;

  public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);
    //config.setTraceMessage(true);

    try {
 
      connection = Connector.newConnection(config);
 
      // display some current settings
      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
      System.out.println("Username: "+config.getUsername());
      System.out.println("SessionId: "+config.getSessionId());
 
      // run the different examples
      queryContacts();
      createAccounts();
      updateAccounts();
      deleteAccounts();
 
 
    } catch (ConnectionException e1) {
        e1.printStackTrace();
    }

  }

  // queries and displays the 5 newest contacts
  private static void queryContacts() {

    System.out.println("Querying for the 5 newest Contacts...");

    try {
   
      // query for the 5 newest contacts  
      QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
       "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
      if (queryResults.getSize() > 0) {
        for (int i=0;i<queryResults.getRecords().length;i++) {
          // cast the SObject to a strongly-typed Contact
          Contact c = (Contact)queryResults.getRecords()[i];
          System.out.println("Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
              c.getLastName()+" - Account: "+c.getAccount().getName());
        }
      }
 
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

  // create 5 test Accounts
  private static void createAccounts() {

    System.out.println("Creating 5 new test Accounts...");
    Account[] records = new Account[5];

    try {
   
      // create 5 test accounts
      for (int i=0;i<5;i++) {
        Account a = new Account();
        a.setName("Test Account "+i);
        records[i] = a;
      }
 
      // create the records in Salesforce.com
      SaveResult[] saveResults = connection.create(records);
 
      // check the returned results for any errors
      for (int i=0; i< saveResults.length; i++) {
        if (saveResults[i].isSuccess()) {
          System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId());
        } else {
          Error[] errors = saveResults[i].getErrors();
          for (int j=0; j< errors.length; j++) {
            System.out.println("ERROR creating record: " + errors[j].getMessage());
          }
        }
      }
 
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}
/********************************************************************************/
Helpful Links:
http://wiki.developerforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector
http://code.google.com/p/sfdc-wsc/downloads/list

http://boards.developerforce.com/t5/Java-Development/Trying-to-call-a-simple-Apex-Web-Service-method-from-Java/td-p/206407
http://boards.developerforce.com/t5/General-Development/How-to-Call-WebService-Method-in-Java/td-p/261755
http://kperisetla.blogspot.com/2011/09/creating-custom-apex-web-service-in.html
http://forums.crmsuccess.com/t5/forums/forumtopicprintpage/board-id/JAVA_development/message-id/5930/print-single-message/false
http://boards.developerforce.com/t5/Java-Development/Problem-Calling-Apex-WebService-from-Java/td-p/206637
http://stackoverflow.com/questions/11204614/access-salesforce-apex-soap-webservice-from-java

Thursday, May 30, 2013

Java Basics

The Java Programming Language and the Java Platform

Java is a high-level language and software-only platform. It runs on more than 50 million personal computers and on billions of devices worldwide. 9 million developers have created Java applications in all major industries. Check out examples of those applications at Java in Action  

The two main components of the Java platform are the Java Application Programming Interface (API), which is a library of Java command lines and the Java Virtual Machine (JVM) that interprets Java code into machine language.  

The same application can run on multiple platforms 

JVM
The API and the JVM make the program independent from the underlying hardware
Java Platform

Learn about the JVM and the APIs

Jump into Java

Code a “Hello World” application in Java.  Follow these step-by-step instructions to quickly create your first application using a text editor in Windows or with NetBeans Integrated Development Environment (IDE). 
Learn Java programming with The Java Tutorials, a comprehensive source for Java language concepts.  Object-oriented and Java programming are explained with graphics and code examples. 
Pick the right tool.  Professionals use NetBeans IDE, an Oracle full-featured Java development software program and JavaFX, a client platform to create and deploy rich Internet applications. Beginners use visual educational tools such as Alice, Greenfoot and BlueJ.  The Young Developers  page summarizes the tools and learning resources for each software tool.