在Salesforce中创建了自己需要用到的对象后,我们想要在别的应用中读写纪录到对象中,首先需要的是自己Salesforce平台的权限通过。登陆自己的Salesforce,下载WSDL文件。
依次点击右上角你的名字中设置--》应用程序设置--》发展--》API。
如果你的是英文,那么依次点击 Your Name --》 Setup --》 App Setup --》 Develop --》 API。
到了如下页面后,下载图中的三个WSDL文件。
这里官方的方法是下载github中的Salesforces的WSC项目 https://github.com/forcedotcom/wsc 在github中也有WSC把wsdl文件生成jar的教程,但是当我执行到 mvn clean package 清理WSC项目的时候便进行不下去了,是因为下载某些包的时候链接是外网,尽管有FQ,但还是更新失败,而且速度很慢,最后只有失败告终。于是我去寻找另外的方法。究其原理,不过也就是为了获取一个WSC的jar,然后通过cmd的命令来构建wsdl文件的jar文件,然后把这些jar文件引入到java项目中,便可以读写Salesforce中的对象。
在这个链接 <a title="http://code.google.com/p/sfdc-wsc/downloads/list" href="http://code.google.com/p/sfdc-wsc/downloads/list">http://code.google.com/p/sfdc-wsc/downloads/list</a> 中下载wsc的jar文件。其中我下载的是wsc-23.jar文件。如果打不开网址,需要翻一下墙,下载如果不能继续,试下右键 用迅雷下载,我的就是用迅雷下载的,如果有会员,那最多十几秒的事情。
下载完成后,便是把wsdl文件编译成对应jar。这里我用windows系统。把刚才下载的jar包和3个wsdl文件放在同一个文件夹中,打开cmd黑色的命令窗,先用cd命令进入到对应的文件夹中。接下来cmd命令窗口中输入如下:
java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc partner.wsdl partner.jar java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc metadata.wsdl metadata.jar
这里需要先安装java的jdk,本来是开发java的程序来读写Salesforce的对象和数据,我就当大家都已经装好了jdk的了。如果执行没有问题,那么你的文件夹应该多了3个jar文件,最后整个文件夹便变成:
准备就绪,接下来想开发任何程序都可以了,web,server,安卓(安卓和IOS有另外的SDK,没必要用这种方法,个人理解)等等都行了。
创建java程序和引用jar外部包我就不解释,要应用的jar就是上图文件夹中的4个jar,对,wsc-23.jar也要
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"; //Salesforce账号中的用户名 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); try { connection = Connector.newConnection(config); // 增删改查 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(); } } }
例子中如果需要用到新的对象,比如我自己创建的Position对象,那么可以在对象的详细页面找到对象对应的api名称,以及对象中字段api名称,如下图
找到对应的api名称后,如果需要创建一条Position纪录,那么可以这样new一个对象 Position__c a = new Position__c(); 然后通过Set的扩展方法来对对象字段的值进行赋值便可。