Jeff Douglas -- 一个SF开发者的Blog,记录一下

[url]http://blog.jeffdouglas.com/2010/05/17/java-command-line-app-using-the-salesforce-wsc/[/url]

The Force.com Web Service Connector (WSC) is a high performance web services stack that is much easier to implement than the “tried and true” Force.com Web Services API. Here’s a quick command line app you can use as a starter application. This class simply creates a new Account and then queries for the 5 newest Accounts by created date.

To get started, download wsc-18.jar from the WSC project’s download page to your desktop (any location will do). Now log into your Developer org and download the Partner WSDL (Setup -> App Setup -> Develop -> API) to your desktop as “partner.wsdl”. Now we’ll need to generate the stub code from the Partner WSDL. Make sure you have Java 1.6 installed and open a command prompt. Now run wsdlc on the Partner WSDL you just downloaded (detailed instruction are here):

java -classpath wsc-18.jar com.sforce.ws.tools.wsdlc partner.wsdl partner.jar

This will create a “partner.jar” file on your desktop. If you are using a Sandbox instead of a Developer or Production org, here are instructions for running wsdlc as there are a few issues. Your console should look similar to:


[img]http://dl.iteye.com/upload/attachment/607901/dfbb04b5-2e3d-3466-8b34-f1467e7637ce.jpg[/img]


Note: You can also simply download the partner-18.jar and bypass the steps above to generate the partner.jar.

Now create a new Java project in Eclipse, add the wsc-18.jar and partner.jar files to your build path and copy the code below. You’ll need to add your username and password/security token before you run the code. Running the code should produce output similar to:

Here’s the starter code for the application.

package com.jeffdouglas;

import com.sforce.soap.partner.*;
import com.sforce.soap.partner.sobject.*;
import com.sforce.ws.*;

public class Main {

public static void main(String[] args) {

ConnectorConfig config = new ConnectorConfig();
config.setUsername("YOUR-USERNAME");
config.setPassword("YOUR-PASSWORD-AND-SECURITYTOKEN");

PartnerConnection connection = null;

try {

// create a connection object with the credentials
connection = Connector.newConnection(config);

// create a new account
System.out.println("Creating a new Account...");
SObject account = new SObject();
account.setType("Account");
account.setField("Name", "ACME Account 1");
SaveResult[] results = connection.create(new SObject[] { account });
System.out.println("Created Account: " + results[0].getId());

// query for the 5 newest accounts
System.out.println("Querying for the 5 newest Accounts...");
QueryResult queryResults = connection.query("SELECT Id, Name from Account " +
"ORDER BY CreatedDate DESC LIMIT 5");
if (queryResults.getSize() > 0) {
for (SObject s: queryResults.getRecords()) {
System.out.println("Id: " + s.getField("Id") + " - Name: "+s.getField("Name"));
}
}

} catch (ConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值