salesforce - 需要使用 HTTP POST 请求从salesforce向外部服务器/另一个salesforce组织发送xml数据

  显示原文与译文双语对照的内容
0 0

我在学习 REST api,在这里我可以发布单个记录。 现在我已经用json格式为 GET/POST/PUT 发送了数据/params,但是现在我想处理大量的调用。 有一个例子是同样的。"。"。我浏览了它,但是我想如何开始。 先谢谢了

时间:原作者:1个回答

0 0

下面是一个xml示例:

@RestResource(urlMapping='/GETSaamTestSearches/*')
global with sharing class GETSaamTestSearches
{
 @HttpGet
 global static void doGet() {
 string sResOutPut = '';
 try {
 RestRequest req = RestContext.request;
 RestResponse res = RestContext.response;
 res.addHeader('Content-Type', 'application/xml');
 SVendorId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
 sResOutPut = '<?xml version='1.0' encoding='UTF-8'?>';
 sResOutPut += '<SearchestoProcess>';
 date dCurrentDate = System.today();
 sResOutPut += '<records>';
 for (Background_Search__c oBGS : [SELECT Id,.... FROM SalesforceObject Limit 500] ) {
 sResOutPut += '<row>';
//************************************
 sResOutPut += '<SubjectAddress>'+ChecknullString(oBGS.Subject_Address__c)+'</SubjectAddress>';
 sResOutPut += '<SubjectCity>'+ChecknullString(oBGS.Subject_City__c)+'</SubjectCity>';
 sResOutPut += '<SubjectState>'+ChecknullString(oBGS.Subject_State__c)+'</SubjectState>';
 sResOutPut += '<SubjectZipCode>'+ChecknullString(oBGS.Subject_Zip_Code__c)+'</SubjectZipCode>';
 sResOutPut += '<SearchTypeName>'+ChecknullString(oBGS.Search_Type_Name__c)+'</SearchTypeName>'; 
 sResOutPut += '<State>'+ChecknullString(oBGS.Jurisdiction__r.State_Name__c)+'</State>'; 
 sResOutPut += '<TimeFrame>7</TimeFrame>'; 
 sResOutPut += '<NotesToResearcher>'+ChecknullString(oBGS.Notes_Pre_to_Researcher__c)+'</NotesToResearcher>'; 
 sResOutPut += '<SearchID>'+ChecknullString(oBGS.ID)+'</SearchID>'; 
//************************************
 sResOutPut += '</row>';
 }
 sResOutPut += '</records>';
 sResOutPut += '<message>Success</message>';
 sResOutPut += '</SearchestoProcess>';
//*******test the generate xml is correct or not by loading it in Dom document
 Dom.Document domDoc = new Dom.Document();
 domDoc.load(sResOutPut);
 }
 catch(exception ex) {
 sResOutPut = '<?xml version='1.0' encoding='UTF-8'?><SearchestoProcess><message>';
 sResOutPut += 'Error while processing the records +'+ex.getmessage()+'</message></SearchestoProcess>';
 }
 RestContext.response.responseBody = Blob.valueOf(sResOutPut);
 }
 public static string SVendorId {get; private set;}
 public static string ChecknullString(string sVal)
 {
 if(sVal == null) {
 sVal = '';
 }
 else {
//*****XML scape charactor
 sVal = sVal.Replace('&', '&amp;').Replace('>', '&gt;').Replace('<', '&lt;').Replace(''', '&apos;').Replace('"', '&quot;');
 }
 return sVal;
 }
 private static string ChecknullNFormatDate(date sVal)
 {
 string sRetVal = ''; 
 if(sVal == null) {
 sRetVal = '';
 }
 else {
 sRetVal = sVal.format();
 }
 return sRetVal;
 }
}
原作者:
...