我试着总结学习,比如Salesforce等系统通过SOAP/REST API实时链接时的设置方法。
Salesforce → 远程系统(= 外部系统)
当 Salesforce 调用远程系统(=外部系统)的 SOAP/REST API 时
Ex. Salesforce传递订单号,订单号,外部系统返回订单详细信息的情况
对于 REST API(Http 标注)
・需要在 Salesforce 端注册远程站点(注册可以通信的 URL)
・创建一个 ApexClass。 (使用 HttpRequest 进行身份验证 + 调用。)
对于 SOAP API
・需要在 Salesforce 端注册远程站点(注册可以通信的 URL)
・通过从远程系统获取 WSDL 并导入它,自动生成 ApexClass 用于标注。
・认证时,使用上述 ApexClass 方法,获取会话 ID。
远程站点注册
Salesforce ← 远程系统(= 外部系统)
当远程系统(=外部系统)调用 Salesforce 的 SOAP/REST API 时
Ex. 外部系统传递客户编号,即客户密钥,Salesforce返回客户详细信息的情况
对于 REST API
・在远程系统上创建一个调用 Salesforce RESTAPI 的类。
・认证由 Salesforce 的连接应用程序管理。 (OAuth)
对于 SOAP API
・从 Salesforce 获取 WSDL 并将其传递给远程系统。
・对于认证,对方系统使用通过导入WSDL生成的类方法并获取会话ID。
・如果远程系统的 IP 没有在组织、个人资料等中注册,除了 PW 之外,还要传递安全令牌。
WSDL
种类(用于 SOAP API)
使用 SOAP API,调用 API 的一方生成 WSDL 并在相对的系统方读取它,
关于 WSDL,在 Salesforce 的情况下,有以下两种方法。
・企业→适合单个组织使用
・合作伙伴→最适合多个组织使用
*选择 SOAP API 时,每次更改组织设置时都需要重新加载 WSDL。
*REST API 不需要 WSDL。
在真实设备上尝试来自 Salesforce 的 CallOut (REST)
使用 Restapi 对端点执行 Apexcallout。
获取majestic badger等数值。
https://th-apex-http-callout.herokuapp.com/animals?_ga=2.17284665.1052735684.1664665841-1174435908.1664505734
(1) 注册到远程站点
| 远程站点名称 | 动物_http |
| 远程站点的 URL | https://th-apex-http-callout.herokuapp.com |
▼Salesforce设置屏幕截图
(2)在开发者控制台中选择“Open Execute Anonymous Window”
(3) 复制/粘贴以下内容,勾选OpenLog的选项,执行(执行CallOut)
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if(response.getStatusCode() == 200) {
// Deserialize the JSON string into collections of primitive data types.
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
// Cast the values in the 'animals' key as a list
List<Object> animals = (List<Object>) results.get('animals');
System.debug('Received the following animals:');
for(Object animal: animals) {
System.debug(animal);
}
}
(4) 由于返回返回结果,选择Debug Only并确认
在真机上尝试来自 Salesforce 的 CallOut (SOAP)
将数字传递到下面的端点(1 和 2),使用 SOAPAPI 执行 ApexCallOut,
得到加法结果(3)。
https://th-apex-http-callout.herokuapp.com/animals?_ga=2.17284665.1052735684.1664665841-1174435908.1664505734
(1) 注册到远程站点
| 远程站点名称 | 动物肥皂 |
| 远程站点的 URL | https://th-apex-soap-service.herokuapp.com |
(2) 获取并保存 XML 格式的 WSDL
https://trailhead.salesforce.com/ja/content/learn/modules/apex_integration_services/apex_integration_soap_callouts
(3) 将上述XML导入Salesforce,生成ApexClass
解析 WSDL 并单击生成 Apex 代码
计算器服务类用于同步标注。 AsyncCalculatorServices 类用于异步标注。
(4) 在开发者控制台的“”中选择OpenLog并执行
calculatorServices.CalculatorImplPort calculator = new calculatorServices.CalculatorImplPort();
Double x = 1.0;
Double y = 2.0;
Double result = calculator.doAdd(x,y);
System.debug(result);
(5) 选择Debug Only显示3.0(1.0和2.0相加结果)
真机远程调用Salesforce API
假设您将调用由 Salesforce 创建的自定义 Web 服务,而不是远程调用 Salesforce 标准 API。
(参考)Web服务源码配置
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {
@HttpGet
global static Account getRecord() {
// Add your code
}
}
全局···全局定义方法
@HttpGet 注解在 GET 请求时调用
getRecord・・・自定义 RESTAPI 调用
*REST 端点是https://yourInstance.my.salesforce.com/services/apexrest/是。
(1) 下面,创建一个 Apex 类
@RestResource(urlMapping='/Cases/*')
global with sharing class CaseManager {
@HttpGet
global static Case getCaseById() {
RestRequest request = RestContext.request;
// grab the caseId from the end of the URL
String caseId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case result = [SELECT CaseNumber,Subject,Status,Origin,Priority
FROM Case
WHERE Id = :caseId];
return result;
}
@HttpPost
global static ID createCase(String subject, String status,
String origin, String priority) {
Case thisCase = new Case(
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
insert thisCase;
return thisCase.Id;
}
@HttpDelete
global static void deleteCase() {
RestRequest request = RestContext.request;
String caseId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
delete thisCase;
}
@HttpPut
global static ID upsertCase(String subject, String status,
String origin, String priority, String id) {
Case thisCase = new Case(
Id=id,
Subject=subject,
Status=status,
Origin=origin,
Priority=priority);
// Match case by Id, if present.
// Otherwise, create new case.
upsert thisCase;
// Return the case ID.
return thisCase.Id;
}
@HttpPatch
global static ID updateCaseFields() {
RestRequest request = RestContext.request;
String caseId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
Case thisCase = [SELECT Id FROM Case WHERE Id = :caseId];
// Deserialize the JSON string into name-value pairs
Map<String, Object> params = (Map<String, Object>)JSON.deserializeUntyped(request.requestbody.tostring());
// Iterate through each parameter field and value
for(String fieldName : params.keySet()) {
// Set the field and value on the Case sObject
thisCase.put(fieldName, params.get(fieldName));
}
update thisCase;
return thisCase.Id;
}
}
(2) 从 Workbench 登录调用 Salesforce 网络服务
https://workbench.developerforce.com/login.php?_ga=2.47013159.1052735684.1664665841-1174435908.1664505734
(3)选择REST Explorer,指定POST,endpoint,rewrite Request,Execute
(4) 创建 (POST) 案例!
(参考)虽然连接的应用程序没有显式注册(仅限许可),但Workbench的连接应用程序已自动安装。
奖金
・SOAP 和 REST 哪个更好?
https://trailhead.salesforce.com/ja/content/learn/modules/apex_integration_services/apex_integration_callouts#apex_integration_callouts_authorizing
> 对于那些想知道“我应该使用哪一个?”的人,请尽可能使用 HTTP 服务。这些服务通常更容易使用,需要的代码少得多,并且可以利用可读的 JSON。在过去的几年里,每个“酷孩子”都转向了 REST 服务,但这并不意味着 SOAP Web 服务不好。 SOAP Web 服务有很长的历史(在 Internet 年代),主要用于企业应用程序。这些不会很快消失。 SOAP 将主要用于与遗留应用程序的集成以及需要正式交换格式或有状态操作的事务。该模块还简要介绍了 SOAP,但大部分时间都花在了 REST 上。
ByTrailhead 截至 2022 年 8 月 10 日
・数以千计或更多的大量数据的批量 API
原创声明:本文系作者授权爱码网发表,未经许可,不得转载;
原文地址:https://www.likecs.com/show-308628613.html