使用java调用 salesforce SOAP API

可以从github上下载最新版本然后用maven工具构建

下面列下我用wsc工具打成jar包测试过程中的代码:

java -cp force-wsc-27.0.0-jar-with-dependencies.jar com.sforce.ws.tools.wsdlc enterprise.wsdl Salesforceenterprise.jar
java -cp force-wsc-27.0.0-jar-with-dependencies.jar com.sforce.ws.tools.wsdlc enterprise.wsdl Salesforceenterprise.jar
[WSC][wsdlc.run:320]Created temp dir: C:\Users\SHENG~1.CHE\AppData\Local\Temp\wsdlc-temp-4791251528898326406-dir
[WSC][wsdlc.:81]Generating Java files from schema ...

[WSC][wsdlc.:81]Generated 374 java files.

[WSC][wsdlc.compileTypes:270]Compiling to target 1.6...

[WSC][wsdlc.compileTypes:270]Compiled 377 java files.

[WSC][wsdlc.:91]Generating jar file ... Salesforceenterprise.jar
[WSC][wsdlc.:91]To include runtime classes in the generated jar please set system property standalone-jar=true
[WSC][wsdlc.:91]Generated jar file Salesforceenterprise.jar
[WSC][wsdlc.run:320]Delete temp dir: C:\Users\SHENG~1.CHE\AppData\Local\Temp\wsdlc-temp-4791251528898326406-dir
[WSC][wsdlc.run:320]Set system property del-temp-dir=false to not delete temp dir.

[WSC][wsdlc.run:320]Completed enterprise.wsdl in (ms) 17833

测试中把其中下载的wsdl文件也放在wsc的target目录了 \成功生成jar包后,将生成后的jar包和force-wsc-xx.jar包一起导入eclipse。

然后就可以用eclipse来操作salesforce中的对象了。

以下是一个测试用例,可以将Acount对象更改成你想操作的任意对象

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// 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;iAccount 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// 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// 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();
    }

  }

}

().length;i++)>().length;i++)>().length;i++)>

Original: https://www.cnblogs.com/csophys/archive/2013/03/08/2950419.html
Author: csophys
Title: 使用java调用 salesforce SOAP API

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/715087/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球