在上一篇博文中,介绍了Springboot连接Salesforce的步骤和环境构建。
其中,只给出了对Salesforce数据进行查询的例子,这篇文章针对Salsforce数据的Update和Create,再展开一下。
对于Create和Update的操作,请求的方式和URL是不同于查询的。
详见Salesforce的官方文档。(Rest API Developer Guide)
1. 创建(Create)一条新的Salesforce数据
请求方法:POST
请求URL:(以Account为例)https://MyDomainName.my.salesforce.com/services/data/v56.0/sobjects/Account/
代码:in SalesforceDataService.java
public Map createSalesforceData() {SalesforceAuthenticator salesforceAuthenticator = SalesforceAuthenticator.getSalesforceToken();try {RestTemplate restTemplate = new RestTemplate();// set urifinal String baseUrl = salesforceAuthenticator.instanceUrl + "/services/data/v52.0/sobjects/Account/";URI uri = new URI(baseUrl);// request headerHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.add(HttpHeaders.AUTHORIZATION, String.format("Bearer %s", salesforceAuthenticator.accessToken));// request bodyMap<String, Object> accCreate = new HashMap<>();accCreate.put("Name", "Create from Java");accCreate.put("Phone", "080-1234-5678");ObjectMapper mapper = new ObjectMapper();String requestBody = mapper.writeValueAsString(accCreate);System.out.println("requestBody = " + requestBody); // build the requestHttpEntity<String> entity = new HttpEntity<>(requestBody, headers);ResponseEntity<Map> response = null;try {response = restTemplate.exchange(uri, HttpMethod.POST, entity, Map.class);System.out.println("createAccount StatusCode = " + response.getStatusCode()); } catch (HttpClientErrorException e) {System.out.println("HttpClient Error : " + e.getMessage());}return response.getBody();} catch (Exception e) {System.out.println("System error : " + e.getMessage());}return Collections.emptyMap();}
注意,Response返回201,Body会返回已经创建好的数据的ID。
{"id" : "001D000000IqhSLIAZ","errors" : [ ],"success" : true
}
2. 更新(Update)一条Salesforce的数据
请求方法:PATCH
请求URL:(以Account为例)
https://MyDomainName.my.salesforce.com/services/data/v56.0/sobjects/Account/001D000000INjVe
注意:Java的HttpClient
是不支持PATCH
方式的。所以,这里有两种方法可以参考:
- 可以根据官方的文档,重写
PostMethod
方法 - 我在网上找到了另一种方式,可以给
restTemplate
设定一下RequestFactory
代码如下:in SalesforceDataService.java
public void updateSalesforceData(String id) {SalesforceAuthenticator salesforceAuthenticator = SalesforceAuthenticator.getSalesforceToken();try {RestTemplate restTemplate = new RestTemplate();// set RequestFactory for PATCH requestHttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();requestFactory.setConnectTimeout(2000); restTemplate.setRequestFactory(requestFactory);// set urifinal String baseUrl = salesforceAuthenticator.instanceUrl + "/services/data/v52.0/sobjects/Account/"+ id;URI uri = new URI(baseUrl);// request headerHttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.add(HttpHeaders.AUTHORIZATION, String.format("Bearer %s", salesforceAuthenticator.accessToken));// request bodyMap<String, Object> accUpdate = new HashMap<>();accUpdate.put("Upsert_test__c", "Update from Java");accUpdate.put("ForTestItem__c", "Spring Boot 101");ObjectMapper mapper = new ObjectMapper();String requestBody = mapper.writeValueAsString(accUpdate);System.out.println("requestBody = " + requestBody); // build the requestHttpEntity<String> entity = new HttpEntity<>(requestBody, headers);ResponseEntity<Map> response = null;try {response = restTemplate.exchange(uri, HttpMethod.PATCH, entity, Map.class);System.out.println("updateAccount StatusCode = " + response.getStatusCode()); } catch (HttpClientErrorException e) {System.out.println("HttpClient Error : " + e.getMessage());}} catch (Exception e) {System.out.println("System error : " + e.getMessage());}return;}
注意,Response返回204,是没有Body的。
还有一点强调的是,Update的URL需要传入你要更新的Record ID,所以每次只能更新一条数据。如果数据不多,可以循环调用这个API。官方的说明是,如果更新的数据超过100条,最好就不要选择这种方式了,要尽量使用Bulk API。
还有Delete,因为比较简单,操作也类似,就不举例了。
以上