内容参考:
https://resources.docs.salesforce.com/202/latest/en-us/sfdc/pdf/salesforce_developer_environment_tipsheet.pdf
https://www.cnblogs.com/zero-zyq/p/6077773.html
具体步骤:
1.下载Force.com Web Service Connector
下载链接为http://mvnrepository.com/artifact/com.force.api/force-wsc
2.下载开发的WSDL文件
Setup->Build->API,这里有很多可以选择的WSDL,企业级WSDL,合作伙伴,元信息等。此处选择企业级WSDL,选择以后详情页右键另存为MyProject.wsdl,名字随便起
3.下载Rhino JavaScript framework,下载完rar文件以后选择其中的js.jar
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Download_Rhino
4.下载StringTemplate engine framework ,我选择的是目前最新的4.0.8.jar
http://www.stringtemplate.org/download.html
5.下载ANTRL runtime,我下载的是最新的版本的complete模式,其他模式有可能缺少相关的类
http://www.antlr.org/download.html
6.生成myproject.jar,将上述jar包以及myproject.wsdl放置在C:\Users\test\Desktop\myproject目录下,打开控制台cmd
java -classpath force-wsc-42.0.0.jar;ST4-4.0.8.jar;antlr-4.7.1-complete.jar;js.jar com.sforce.ws.tools.wsdlc phonesalesystem.wsdl phonesalesystem.jar
7.使用force-wsc-38.04.jar以及生成的myproject.jar实现java通过soap api方式访问sfdc,使用soap api访问有很多限制,比如一次insert或者upsert不能超过200等,这里举例登录以及批量添加功能。
SOAP API : https://resources.docs.salesforce.com/204/latest/en-us/sfdc/pdf/apex_api.pdf
- public class Test {
- static final String USERNAME = "xxx";
- static final String PASSWORD = "xxxx";
- static EnterpriseConnection connection;
- static List<Brand__c> brandList;
- public static void main(String[] args) throws NoSuchAlgorithmException {
- ConnectorConfig config = new ConnectorConfig();
- config.setUsername(USERNAME);
- config.setPassword(PASSWORD);
- try {
- //SSLContext CTX = SSLContext.getInstance("SSL");
- //SSLContext.setDefault(CTX);
- // SSLContext sc = SSLContext.getInstance("SSLv3");
- //String result = httpsRequest("https://blog.peos.cn","GET",null);
- //System.out.println(result);
- String authEndPoint = "https://login.salesforce.com/services/Soap/c/42.0";
- //System.out.println("AuthEndPoint: " + authEndPoint);
- config.setAuthEndpoint(authEndPoint);
- @SuppressWarnings("static-access")
- Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));
- config.setProxy(proxy);
- connection = new EnterpriseConnection(config);
- connection = Connector.newConnection(config);
- 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());
- // brandList =
- querySample();
- } catch ( Exception e1) {
- e1.printStackTrace();
- }
- }
- private static void querySample() {
- String soqlQuery = "SELECT FirstName, LastName FROM Contact";
- try {
- QueryResult qr = connection.query(soqlQuery);
- boolean done = false;
- if (qr.getSize() > 0) {
- System.out.println("\nLogged-in user can see " + qr.getRecords().length + " contact records.");
- while (!done) {
- System.out.println("");
- Contact[] records = (Contact[]) qr.getRecords();
- for (int i = 0; i < records.length; ++i) {
- Contact con = (Contact) records[i];
- String fName = con.getFirstName();
- String lName = con.getLastName();
- if (fName == null) {
- System.out.println("Contact " + (i + 1) + ": " + lName);
- } else {
- System.out.println("Contact " + (i + 1) + ": " + fName + " " + lName);
- }
- }
- if (qr.isDone()) {
- done = true;
- } else {
- qr = connection.queryMore(qr.getQueryLocator());
- }
- }
- } else {
- System.out.println("No records found.");
- }
- } catch (ConnectionException ce) {
- ce.printStackTrace();
- }
- }
- private static String proxyHost = "10.248.60.204";
- private static int proxyPort = 3266;
- }
常见问题:
1.JDK版本为1.6,1.7的,会报STL1.2的错误,请升级JDK为1.8+
2.升级JDK仍然报错的,请使用代理链接(见代码,我就是因为没有设置代码,一直以为是STL没有配置好走了很久的弯路)
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · salseforce apex调用salesforce soap api
- · Salseforce Ant Migration Tool
- · Salesforce VisualForce 使用apex:actionFunction调用后台方法
- · Filter Picklist Values Based on Record Type in Lightning
- · How to Call Salesforce API from Lightning Component
- · salesforce lightning icon
- · apex highlight userDefineLang for notepad++
- · 详解 Salesforce 15 和 18 位的ID
- · Salesforce Test class注意事项
- · salesforce Milestone clock is not running
- · salesforce apex traced the number of query
- · salesforce执行apex job的两种方法