Tuesday, November 11, 2014

JAVA Salesforce integration

How to integrate Salesforce with Java 


1.       Generate WSDL from Salesforce organization.
2.       Download Java library Link
3.       Create WSDL jar file following command

"C:\Program Files\Java\jdk1.7.0_65\bin\java.exe" -Dpackage-prefix=wsc -classpath force-wsc-29.0.0.Jar;js-1.7R2.jar;stringtemplate-4.0.2.jar;antlr-complete-3.5.1.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

[WSC][Generator.generate:130]Generating Java files from schema...
[WSC][Generator.generate:130]Generated 529 java files.
[WSC][Generator.compileTypes:126]Compiling to target 1.6...
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
[WSC][Generator.compileTypes:126]Compiled 532 java files.
[WSC][wsdlc.run:111]Generating jar file ... enterprise.jar
[WSC][wsdlc.run:111]Generated jar file enterprise.jar

4.       Create Java Project
5.       Includes Java library for Salesforce
a.       antlr-complete-3.5.1.jar
b.      stringtemplate-4.0.2.jar
c.       js-1.7R2.jar
d.      force-wsc-29.0.0.jar
6.       Java code for Create ,update, delete
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sfjava;

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

public class SFJava {

static final String USERNAME = "gouranga@force.com";
static final String PASSWORD = "watermelon21JGzfzbaD9a24aUrIodyOVK9cN";
static EnterpriseConnection connection;
       
    public static void main(String[] args)
    {
       ConnectorConfig config = new ConnectorConfig();
                    config.setUsername(USERNAME);
                    config.setPassword(PASSWORD);
                    //config.setTraceMessage(true);
              System.out.println("Password" + PASSWORD );
                      System.out.println("USERNAME" + USERNAME );
                    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();
                    }  
                    
                  }
                  
                  // updates the 5 newly created Accounts
                  private static void updateAccounts() {
                    
                    System.out.println("Update the 5 new test Accounts...");
                    Account[] records = new Account[5];
                    
                    try {
                       
                      QueryResult queryResults = connection.query("SELECT Id, Name FROM Account 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 Account
                          Account a = (Account)queryResults.getRecords()[i];
                          System.out.println("Updating Id: " + a.getId() + " - Name: "+a.getName());
                          // modify the name of the Account
                          a.setName(a.getName()+" -- UPDATED");
                          records[i] = a;
                        }
                      }
                      
                      // update the records in Salesforce.com
                      SaveResult[] saveResults = connection.update(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 updated record - Id: " + saveResults[i].getId());
                        } else {
                          Error[] errors = saveResults[i].getErrors();
                          for (int j=0; j< errors.length; j++) {
                            System.out.println("ERROR updating record: " + errors[j].getMessage());
                          }
                        }  
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
                  
                  // delete the 5 newly created Account
                private static void deleteAccounts() {
                    
                    System.out.println("Deleting the 5 new test Accounts...");
                    String[] ids = new String[5];
                    
                    try {
                       
                      QueryResult queryResults = connection.query("SELECT Id, Name FROM Account 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 Account
                          Account a = (Account)queryResults.getRecords()[i];
                          // add the Account Id to the array to be deleted
                          ids[i] = a.getId();
                          System.out.println("Deleting Id: " + a.getId() + " - Name: "+a.getName());
                        }
                      }
                      
                      // delete the records in Salesforce.com by passing an array of Ids
                      DeleteResult[] deleteResults = connection.delete(ids);
                      
                      // check the results for any errors
                      for (int i=0; i< deleteResults.length; i++) {
                        if (deleteResults[i].isSuccess()) {
                          System.out.println(i+". Successfully deleted record - Id: " + deleteResults[i].getId());
                        } else {
                          Error[] errors = deleteResults[i].getErrors();
                          for (int j=0; j< errors.length; j++) {
                            System.out.println("ERROR deleting record: " + errors[j].getMessage());
          }
                        }  
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
   
}

Result
run:
Passwordwatermelon21JGzfzbaD9a24aUrIodyOVK9cN
USERNAMEgouranga@force.com
Auth EndPoint: https://login.salesforce.com/services/Soap/c/32.0
Service EndPoint: https://ap1.salesforce.com/services/Soap/c/32.0/00D90000000xqwN
Username: gouranga@force.com
SessionId: 00D90000000xqwN!ARUAQPxajvZ29zWyWR15egV7r_.mbtVuYW.vkQKTsZoiIjLdnK0GKxSFs5jgjej9O6IcuHmWPdrIbdL3AuxcShlQcU_BBARH
Querying for the 5 newest Contacts...
Id: 0039000001BsyyIAAR - Name: Gouranga Sadhukhan - Account: Primalink
Id: 00390000016M9I7AAK - Name: Edna Frank - Account: GenePoint
Id: 00390000016M9I5AAK - Name: Tom Ripley - Account: United Oil & Gas, Singapore
Id: 00390000016M9I6AAK - Name: Liz D'Cruz - Account: United Oil & Gas, Singapore
Id: 00390000016M9HsAAK - Name: Sean Forbes - Account: Edge Communications
Creating 5 new test Accounts...
0. Successfully created record - Id: 00190000018SZ2zAAG
1. Successfully created record - Id: 00190000018SZ30AAG
2. Successfully created record - Id: 00190000018SZ31AAG
3. Successfully created record - Id: 00190000018SZ32AAG
4. Successfully created record - Id: 00190000018SZ33AAG
Update the 5 new test Accounts...
Updating Id: 00190000018SZ2zAAG - Name: Test Account 0
Updating Id: 00190000018SZ33AAG - Name: Test Account 4
Updating Id: 00190000018SZ32AAG - Name: Test Account 3
Updating Id: 00190000018SZ31AAG - Name: Test Account 2
Updating Id: 00190000018SZ30AAG - Name: Test Account 1
0. Successfully updated record - Id: 00190000018SZ2zAAG
1. Successfully updated record - Id: 00190000018SZ33AAG
2. Successfully updated record - Id: 00190000018SZ32AAG
3. Successfully updated record - Id: 00190000018SZ31AAG
4. Successfully updated record - Id: 00190000018SZ30AAG
Deleting the 5 new test Accounts...
Deleting Id: 00190000018SZ2zAAG - Name: Test Account 0 -- UPDATED
Deleting Id: 00190000018SZ33AAG - Name: Test Account 4 -- UPDATED
Deleting Id: 00190000018SZ32AAG - Name: Test Account 3 -- UPDATED
Deleting Id: 00190000018SZ31AAG - Name: Test Account 2 -- UPDATED
Deleting Id: 00190000018SZ30AAG - Name: Test Account 1 -- UPDATED
0. Successfully deleted record - Id: 00190000018SZ2zAAG
1. Successfully deleted record - Id: 00190000018SZ33AAG
2. Successfully deleted record - Id: 00190000018SZ32AAG
3. Successfully deleted record - Id: 00190000018SZ31AAG
4. Successfully deleted record - Id: 00190000018SZ30AAG

BUILD SUCCESSFUL (total time: 29 seconds)

No comments:

Post a Comment