看了看自己前一篇博客还是去年12月份的,看来已经大半年没写博客了。一方面是太忙了,但最重要的还是懒得写了(想想之前还是一周保持两篇的更新速度)。借这次五一长假调查salesforce的web service功能就开启今年的第一篇博客吧!
在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用java的程序进行简单的调用。
在查看网上的多片博客后终于实现。下面记录一下过程。
【注:如果要使用salesforce中的自定义的Apex Class时,要想使其成为web service,那么class一定要定义成global的,具体的方法要用 webService static 修饰】
1):在Salesforce中创建如下Class,例如:
global class SFAccountWebService {
webService static string UpsertAccount(String accountXmlInfo) {
Account currentAcc = GenerateAccountFromXmlInfo(accountXmlInfo);
try
{
Account acc = [Select Id From Account a Where AccountNumber =: currentAcc.AccountNumber];
if(acc != null){
currentAcc.Id = acc.Id;
}
upsert currentAcc;
return 'true';
}
catch(exception ex){
return 'false';
}
}
private static Account GenerateAccountFromXmlInfo(String accountXmlInfo){
Account currentAcc = new Account();
// Parse the xml info to generate the Account Object
return currentAcc;
}
}
2):在保存好上述的class之后,我们到setup --> build --> develop --> apex classes
中找到刚刚保存的class,我们会发现在对应的Action中有WSDL这个选项,此选项就是Salesforce默认所提供的将Web Service的class转化成WSDL文件。如下图所示
3):点击上图的WSDL按钮,会看到如下界面,这里显示的是生成的WSDL文件的详细信息,我们点击鼠标右键,将此文件保存到本地,这里姑且取名为SFAccountWebService.wsdl
接下来就是将SFAccountWebService.wsdl
生成对应的SFAccountWebService.jar
,具体怎么生成就是我接下来要讲的内容,再讲这个之前先讲讲怎么通过Salesforce平台的权限。
在Salesforce中创建了自己需要用到的对象后,我们想要在别的应用中读写纪录到对象中,首先需要的是自己Salesforce平台的权限通过。登陆自己的Salesforce,下载WSDL文件。
Your Name --> Setup --> App Setup --> Develop --> API
。或者直接在搜索框中搜索API。partner.jar
java -classpath antlr-runtime-3.5.2.jar:ST-4.3.jar:tools.jar:force-wsc-49.2.0.jar com.sforce.ws.tools.wsdlc wsdl.jsp.xml partner.jar
enterprise.jar
java -classpath antlr-runtime-3.5.2.jar:ST-4.3.jar:tools.jar:force-wsc-49.2.0.jar com.sforce.ws.tools.wsdlc wsdl.xml enterprise.jar
metadata.jar
java -classpath antlr-runtime-3.5.2.jar:ST-4.3.jar:tools.jar:force-wsc-49.2.0.jar com.sforce.ws.tools.wsdlc metadata.xml metadata.jar
TestWebService
,然后将partner.jar
,metadata.jar
,enterprise.jar
,force-wsc-49.2.0.jar
引入外部Jar包(右键项目,选择Build Path
,然后选择Libraries
,点击Add External JARS
)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();
}
}
}
由于登录时的密码需要拼接Security Token
,但不知道Security Token
是什么,这个时候需要到Salesforce
中重置Security Token
,新的Security Token
会发送到对应的邮箱。
重置Token的网址为:“https://[SalesforceDomainHere]/_ui/system/security/ResetApiTokenEdit?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DPersonalInfo&setupid=ResetApiToken”
获取到对应的Token后直接在代码的密码后面拼接Token即可,如下图:
登录时密码部分需要添加对应的SECURITY-TOKEN
SET UP
的搜索框输入profiles
,点击进入,在列表中找到[System Administrator]
并点击进入[Login IP Ranges]
上,点击New
Start IP Address
终输入0.0.0.0
,End IP Address
中输入255.255.255.255
,点击Save
保存。SFAccountWebService.class
,同样的我们也需要将其生成对应的JAR包,例如:java -classpath antlr-runtime-3.5.2.jar:ST-4.3.jar:tools.jar:force-wsc-49.2.0.jar com.sforce.ws.tools.wsdlc SFAccountWebService.xml SFAccountWebService.jar
然后导入到eclipse的项目中利用即可。