本文已参与「新人创作礼」活动,一起开启掘金创作之路。
简介:SF提供接口供SAP调用,获得请求参数,并返回最终结果。
- 返回单条Response
@RestResource(urlMapping = '/PaymentResultsSync/*')
global class PaymentResultSync {
global class PaymentResults{
global List<PaymentResult> results;
}
global class PaymentResult{
public String BUSINESS_ID;
public String SALESFORCE_ID;
public String TYPE;
public String PAY_TIME;
public String PROC_TYPE;
}
global class Response{
public String TYPE;
public String MSG;
public String BUSINESS_ID;
public String SALESFORCE_ID;
public String PROC_TYPE;
}
@HttpPost
global static void doPost(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
res.addHeader('Content-Type', 'application/json;Charset=UTF-8');
String reqMsg = req.requestBody.toString();
system.debug('josn body: ' + reqMsg);
Response syncResponse = new Response();
Interface_Log__c log = new Interface_Log__c();
log.Name = 'CE付款结果回传';
log.Start_Time__c = Datetime.now().format('yyyy-MM-dd HH:mm:ss');
log.Sync_Content__c = reqMsg;
try{
PaymentResults paymentResults = (PaymentResults)JSON.deserialize(reqMsg, PaymentResultSync.PaymentResults.Class);
system.debug('PaymentResults: ' + paymentResults);
List<Payment_Application__c> paymentApplicationsToUpsert = new List<Payment_Application__c>();
List<Advance_Application__c> advanceApplicationsToUpsert = new List<Advance_Application__c>();
for(PaymentResult result : paymentResults.results){
if(result.PROC_TYPE == 'B04'){
Payment_Application__c pa = new Payment_Application__c();
pa.CE_Number__c = result.BUSINESS_ID;
pa.Payment_Status__c = result.TYPE;
pa.Paid_Date__c = Date.valueOf(result.PAY_TIME);
paymentApplicationsToUpsert.add(pa);
}else if(result.PROC_TYPE == 'C01'){
Advance_Application__c aa = new Advance_Application__c();
aa.CE_Number__c = result.BUSINESS_ID;
aa.Advance_Status__c = result.TYPE;
aa.Payment_Date__c = Date.valueOf(result.PAY_TIME);
advanceApplicationsToUpsert.add(aa);
}
}
upsert paymentApplicationsToUpsert CE_Number__c;
upsert advanceApplicationsToUpsert CE_Number__c;
syncResponse.TYPE = 'S';
syncResponse.MSG = '同步成功';
log.IsSuccess__c = '成功';
}catch(Exception e){
system.debug('payment result sync exception: ' + e.getLineNumber() + ': ' + e.getMessage());
syncResponse.TYPE = 'E';
syncResponse.MSG = e.getMessage();
log.IsSuccess__c = '失败';
log.returnErrMsg__c = '第' + e.getLineNumber() + '行:' + e.getMessage();
}
log.End_Time__c = Datetime.now().format('yyyy-MM-dd HH:mm:ss');
insert log;
res.responseBody = Blob.valueOf(JSON.serialize(syncResponse));
}
}
- 返回所有同步记录的Response List
@RestResource(urlMapping = '/PaymentResultsSync/*')
global class PaymentResultSync {
// 封装数据结构 - 参数
global class PaymentResults{
global List<PaymentResult> results;
}
global class PaymentResult{
public String BUSINESS_ID;
public String SALESFORCE_ID;
public String TYPE;
public String PAY_TIME;
public String PROC_TYPE;
}
// 封装数据结构 - 返回结果
global class SyncResults{
global List<Response> responses;
}
global class Response{
public String TYPE;
public String MSG;
public String BUSINESS_ID;
public String SALESFORCE_ID;
public String PROC_TYPE;
}
@HttpPost
global static void doPost(){
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
res.addHeader('Content-Type', 'application/json;Charset=UTF-8');
String reqMsg = req.requestBody.toString();
system.debug('josn body: ' + reqMsg);
// 实例化返回结果内部类 - List<Response>
SyncResults syncResponseList = new SyncResults();
syncResponseList.responses = new List<Response>();
// 实例化日志信息 - 将结果记录在sf的Interface_Log__c对象
Interface_Log__c log = new Interface_Log__c();
log.Name = 'CE付款结果回传';
log.Start_Time__c = Datetime.now().format('yyyy-MM-dd HH:mm:ss');
log.Sync_Content__c = reqMsg;
try{
// 反序列化请求结果
PaymentResults paymentResults = (PaymentResults)JSON.deserialize(reqMsg, PaymentResultSync.PaymentResults.Class);
system.debug('PaymentResults: ' + paymentResults);
// 声明
List<Payment_Application__c> paymentApplicationsToUpsert = new List<Payment_Application__c>();
List<Advance_Application__c> advanceApplicationsToUpsert = new List<Advance_Application__c>();
// 声明CEN2SFNMap用来保存参数中的BUSINESS_ID(CE_Number__c)和SALESFORCE_ID
Map<String, String> CEN2SFNMap = new Map<String, String>();
for(PaymentResult result : paymentResults.results){
// 建立BUSINESS_ID与SALESFORCE_ID的映射
CEN2SFNMap.put(result.BUSINESS_ID, result.SALESFORCE_ID);
if(result.PROC_TYPE == 'B04'){
Payment_Application__c pa = new Payment_Application__c();
pa.CE_Number__c = result.BUSINESS_ID;
pa.Payment_Status__c = result.TYPE;
pa.Paid_Date__c = Date.valueOf(result.PAY_TIME);
paymentApplicationsToUpsert.add(pa);
}else if(result.PROC_TYPE == 'C01'){
Advance_Application__c aa = new Advance_Application__c();
aa.CE_Number__c = result.BUSINESS_ID;
aa.Advance_Status__c = result.TYPE;
aa.Payment_Date__c = Date.valueOf(result.PAY_TIME);
advanceApplicationsToUpsert.add(aa);
}
}
// 使用Database.upsert更新sf记录 - 主要用于记录成功和失败返回结果的详细信息
List<Database.upsertResult> uPAResults = Database.upsert(paymentApplicationsToUpsert, Payment_Application__c.CE_Number__c, false);
List<Database.upsertResult> uAPRResults = Database.upsert(advanceApplicationsToUpsert, Advance_Application__c.CE_Number__c, false);
// 分别遍利upsert结果
for(Integer i = 0; i < uPAResults.size(); i++) {
Response syncResponse = new Response();
syncResponse.PROC_TYPE = 'B04';
syncResponse.SALESFORCE_ID = CEN2SFNMap.get(paymentApplicationsToUpsert[i].CE_Number__c);
syncResponse.BUSINESS_ID = paymentApplicationsToUpsert[i].CE_Number__c;
if(uPAResults[i].isSuccess()) {
syncResponse.TYPE = 'S';
syncResponse.MSG = '同步成功';
}else {
syncResponse.TYPE = 'E';
syncResponse.MSG = '同步失败';
}
// 向responses中增加返回结果的List
syncResponseList.responses.add(syncResponse);
}
for(Integer i = 0; i < uAPRResults.size(); i++) {
Response syncResponse = new Response();
syncResponse.PROC_TYPE = 'C01';
syncResponse.SALESFORCE_ID = CEN2SFNMap.get(advanceApplicationsToUpsert[i].CE_Number__c);
syncResponse.BUSINESS_ID = advanceApplicationsToUpsert[i].CE_Number__c;
if(uAPRResults[i].isSuccess()) {
syncResponse.TYPE = 'S';
syncResponse.MSG = '同步成功';
}else {
syncResponse.TYPE = 'E';
syncResponse.MSG = '同步失败';
}
// 向responses中增加返回结果的List
syncResponseList.responses.add(syncResponse);
}
log.IsSuccess__c = '成功';
}catch(Exception e){
system.debug('payment result sync exception: ' + e.getLineNumber() + ': ' + e.getMessage());
log.IsSuccess__c = '失败';
log.returnErrMsg__c = '第' + e.getLineNumber() + '行:' + e.getMessage();
}
log.End_Time__c = Datetime.now().format('yyyy-MM-dd HH:mm:ss');
insert log;
res.responseBody = Blob.valueOf(JSON.serialize(syncResponseList));
}
}
- 使用workbench调试第2种接口截图:
a. 正常截图:
b. 异常截图: