• 使用java调用 salesforce SOAP API


    使用java调用 salesforce SOAP API

    使用java调用 salesforce SOAP API

    1 使用java调用 salesforce SOAP API

    1. 下载WSDL文件 可以在salesforce中的设置->发展->API 生成企业WSDL文件处另外为下载。
    2. 将WSDL文件编译成jar包。这里用axis2的wsdl2java工具会有点问题,可以使用salesforce官方的工具wsc来生成jar包。

    wsc工具的使用介绍可以参考:http://wiki.developerforce.com/page/Introduction_to_the_Force.com_Web_Services_Connector

    wsc的官方开发地址为:https://github.com/forcedotcom/wsc

    可以从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.<init>:81]Generating Java files from schema ...
    [WSC][wsdlc.<init>:81]Generated 374 java files.
    [WSC][wsdlc.compileTypes:270]Compiling to target 1.6... 
    [WSC][wsdlc.compileTypes:270]Compiled 377 java files.
    [WSC][wsdlc.<init>:91]Generating jar file ... Salesforceenterprise.jar
    [WSC][wsdlc.<init>:91]To include runtime classes in the generated jar please set system property standalone-jar=true
    [WSC][wsdlc.<init>: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&nbsp;!= 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();
        }    
    
      }
    
    }
    
    

    Date: 2013-03-08 18:44:19 中国标准时间

    Author: csophys

    Org version 7.8.11 with Emacs version 24

    Validate XHTML 1.0
  • 相关阅读:
    浅谈生成全排列的4种方法
    UVA
    UVA
    UVA
    UVA
    MySQL索引篇
    MySQL事务篇
    MySQL架构篇
    Redis性能调优
    Redis分布式锁
  • 原文地址:https://www.cnblogs.com/csophys/p/2950419.html
Copyright © 2020-2023  润新知