Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

XMLHelper with Namespaces

raprohaska
Kilo Guru

ServiceNow Level: Beginner

Trying to parse a SOAP response and I have found all suggestions point to XMLDocument and XMLHelper but I see very little about what functions are exposed through these objects. I think my troubles are due to the namespace in the xml. Here is an example of the xml:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">

  <s:Header />

  <s:Body>
          <CreateIncidentResponse xmlns="http://tempuri.org/">

                  <CreateIncidentResult xmlns:a="http://schemas.datacontract.org/2004/07/Entities.CMS.ExternalServices"
                                                                              xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
         
                            <a:Messages />

                            <a:Success>true</a:Success>

                            <a:TicketID>I140000048</a:TicketID>

                    </CreateIncidentResult>

            </CreateIncidentResponse>

  </s:Body>

</s:Envelope>

var response = s.post();

var helper = new XMLHelper(response);

var obj = helper.toObject();

JSUtil.logObject(obj, 'obj Response: ');

Log Object: obj Response: Object @xmlns:s: string =

http://schemas.xmlsoap.org/soap/envelope/ s:Body: Object UpdateIncidentResponse:

Object @xmlns: string = http://tempuri.org/ UpdateIncidentResult: Object

@xmlns:a: string =

http://schemas.datacontract.org/2004/07/Entities.CMS.ExternalServices

a:Success: string = true a:TicketID: string = I140000048 a:Messages: null = null

@xmlns:i: string = http://www.w3.org/2001/XMLSchema-instance

I can see the values (success = true and TicketID = I140000048) in the object but I don't understand how to access those values from code. Since I can't step through the code it is difficult to understand the structure of the object that is created.

Sorry if I'm reposting a question but I haven't found a great example yet.

- AA

10 REPLIES 10

raprohaska
Kilo Guru


I would actually prefer using XMLDocument using a generic XPATH like "//TicketID" if possible. I'm thinking the object will contain specific properties like CreateIncidentResult. There are a number of SOAP calls with similar response, I would like to have shared code that goes directly to Success and TicketID.



Thanks again,


AA


Hi,



Did you find any solution to your issue.



I am facing the same problem. When trying to capture such a response in code (Business Rule that invokes the web service) Response value in the below code statement does not assign any value to into Response.


var Response = s.post();



Thanks


Tanu


I am also having the same issue.   Did you find a solution?


josh_nerius
ServiceNow Employee
ServiceNow Employee

Hi Ronald / All,



Try using the following syntax:



var ticketId = obj["s:Body"]["CreateIncidentResponse"]["CreateIncidentResult"]["a:TicketID"];




Simple "dot-walking" syntax will not work in this case due to the colons in the element/attribute names.