如何返回XML以响应Apex中的HTTP帖子?

时间:2017-08-31 23:11:27

标签: xml 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 doc并将该字符串作为RestResponse,但两次我都得到了错误&#34;编译错误:Http *方法的类型无效:..&#34;当试图保存它时。

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)

也许答案为时已晚,但是我将解决方案留在这里,因为我遇到了同样的问题,也许其他人也可能遇到同样的问题。

非常简单。您需要将方法设置为void,然后,只需构建响应即可。

检查下面的下一个代码:

@RestResource(urlMapping='/APIClass/*')

global without sharing class Listener {

    @HttpPost
    global static void postMethod() {

        /*
            // All your logic
        */

        // After that, just build the response as needed:
        String xmlResponse = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><notificationsResponse xmlns="http://soap.sforce.com/2005/09/outbound"><Ack>true</Ack></notificationsResponse></soapenv:Body></soapenv:Envelope>'

        RestResponse response = RestContext.response;
        response.addHeader('Content-Type', 'application/xml');
        response.responseBody = Blob.valueOf(xmlResponse);
        response.statusCode = 201;

    }

}

执行该方法时,将生成的响应发送回去。

请尝试在workbench或邮递员的帮助下执行此操作,以查看您的回复。