2017-08-31 43 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(); 
}