2017-08-31 75 views
0

我試圖返回XML來回應一個http post與Apex和我不能爲我的生活弄清楚如何做到這一點。我目前有:如何返回XML以響應Apex中的HTTP發佈?

@RestResource(urlMapping='/routeAPIs/*') 
global class routeAPIController { 
    @HttpPost 
    global static String getOwner(String interation_id, String source_address, String destination_address) { 


     //Get day of the week to check for weekend 
     Boolean dayFlag = false; 
     Date myDate = System.today(); 
     DateTime myDateTime = (DateTime) myDate; 
     String dayOfWeek = myDateTime.format('E'); 
     if(dayOfWeek == 'Sat' || dayOfWeek == 'Sun'){ 
      dayFlag = true; 
     } 

     try{ 
      //Query for the owner using the case number entered 
      Case A = [SELECT OwnerId, Status, CaseNumber FROM Case WHERE Case.ContactPhone =: source_address limit 1]; 
      //Convert OwnerId to string 
      String caseOwner = String.valueOf(A.OwnerId); 
      //Query for the email of the user using the case owner ID 
      User B = [Select Email From User where id = : caseOwner limit 1]; 
      //Convert email to string 
      String ownerEmail = String.valueOf(B.Email); 
      //return xml for successful find of case and owner 
      //Checks for weekend, else weekday output 
      if(dayFlag){ 
       //Checks if most recent case is closed, else case is open 
       if(A.Status == 'Closed'){ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="closed"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
        return xml; 
       } 
       else{ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="open"><Fields><Field name="email">' + ownerEmail + '</Field><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
        return xml; 
       } 
      } 
      else{ 
       //Checks if most recent case is closed, else case is open 
       if(A.Status == 'Closed'){ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="closed"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
        return xml; 
       } 
       else{ 
        String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="open"><Fields><Field name="email">' + ownerEmail + '</Field><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
        return xml; 
       } 
      } 
     } 
     //If case isn't found or not enough numbers were entered 
     catch(QueryException e){ 
      //If no case is found - Routes to weekendResponder/trafficCop depending on day 
      if(dayFlag){ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="newNum"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
       return xml; 
      } 
      else{ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="newNum"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
       return xml; 
      } 
     } 

     //So end of function can't be reached - Routes to weekendResponder/trafficCop depending on day 
     if(dayFlag){ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="wrong"><Fields><Field name="ringGroup">weekendResponder</Field></Fields></Context></Response>'; 
       return xml; 
      } 
      else{ 
       String xml = '<?xml version="1.0" encoding="UTF-8"?><Response><Context output="wrong"><Fields><Field name="ringGroup">trafficCop</Field></Fields></Context></Response>'; 
       return xml; 
      } 
     } 
} 

與當前代碼的問題是它返回一個字符串,所以響應用引號括起來,不正確讀取爲XML。我試圖建立一個XML文件,並把該字符串作爲一個RestResponse,但兩次我收到錯誤「編譯錯誤:無效類型的Http *方法:..」試圖保存它時。

回答

0

This會給你解析XML的很好的例子。要以字符串格式返回,您需要先轉換爲DOM並添加您的子元素,然後使用toXmlString()將XML轉換爲字符串; This會給你如何轉換它

public String toXml() { 

    // Create our top level doc and request node 
    Dom.Document requestDoc = createXMLRequestDocument(); 
    Dom.XmlNode bodyNode = requestDoc.getRootElement().addChildElement('soapenv:Body', null, null); 
    Dom.XmlNode requestMessageNode = bodyNode.addChildElement('addressValidationRequest', null, null); 

    // Add user details 
    Dom.XmlNode userDetailsNode = requestMessageNode.addChildElement('userDetails', null, null); 
    userDetails.addToXmlDoc(userDetailsNode); 

    // Add address 
    Dom.XmlNode addressNode = requestMessageNode.addChildElement('address', null, null); 
    address.addToXmlDoc(addressNode); 

    return requestDoc.toXMLString(); 
}