需要一个使用http post请求将salesforce中的xml数据发送到外部服务器/其他salesforce组织的示例

时间:2014-07-31 10:37:47

标签: salesforce

我正在学习REST apis,我可以在其中发布单个记录。直到现在我已经以json格式(HttpRequest / HttpResponse)发送了GET / POST / PUT的数据/参数,但现在我想处理一个庞大的调用。任何人都有相同的例子?...我用谷歌搜索它但我对如何开始感到困惑...所以如果可能的话想要一个运行代码。提前致谢

1 个答案:

答案 0 :(得分:1)

以下是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;
    }
}