本篇博客主要介绍salesforce SOAP API 和 Metadata API 与java程序的简单集成。如有问题之处,还请各位指出。
中文版本:设置 ——> 开发 ——> API
英文版本:Your Name ——> Setup——> App Setup ——> Develop ——> API
将WSDL文件下载至本地,用此文件打包成jar包引入使用。
网上有不同的方法,我使用的是下载wsc的jar文件,然后使用命令行打成jar包进行使用的,个人感觉还是操作方便简单。其中我下载的是wsc-23.jar文件。
下载地址: http://code.google.com/p/sfdc-wsc/downloads/list
编译jar包 :将wsc-23.jar和3个wsdl文件放在同一个文件夹中,调出命令行窗口,先用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
输出成功后,即可在文件中得到编译好的jar包。导入java项目工程即可。
直接上代码:
package cn.lnc.salesforce.core;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
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;
/**
* @author ningchao.li
*
*/
public class UseSalesforceWSDL {
static final String USERNAME = "*****"; //Salesforce用户名
static final String PASSWORD = "*****"; //密码,这个密码有点特殊,需要在密码后面加入安全标记
static com.sforce.soap.enterprise.EnterpriseConnection connection;
// queries and displays the 5 newest contacts
public static void queryContacts() {
try {
connection = Connector.newConnection(USERNAME,PASSWORD);
} catch (ConnectionException e1) {
e1.printStackTrace();
}
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
public static void createAccounts() {
try {
connection = Connector.newConnection(USERNAME,PASSWORD);
} catch (ConnectionException e1) {
e1.printStackTrace();
}
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
public static void updateAccounts() {
try {
connection = Connector.newConnection(USERNAME,PASSWORD);
} catch (ConnectionException e1) {
e1.printStackTrace();
}
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
public static void deleteAccounts() {
try {
connection = Connector.newConnection(USERNAME,PASSWORD);
} catch (ConnectionException e1) {
e1.printStackTrace();
}
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();
}
}
}
以上代码中定义了增删改查的方法,可以进行直接调用。