Retrieve element from SOAP XML Response

Patrick Quinlan
Giga Guru

Need some help here....

I have an onChange Client Script that runs a SOAP Message script include. The Client Script and Script Include are working, however I'm not sure how to get the specific value I need out of the xmlDoc that is created from the Script Include. 

Normally, I believe xmlDoc.getNodeText would work but with the way the XML document is constructed, it isn't. 

What I'm trying to return from the ScriptInclude is the value for element 'WORK_ORDER_DESCRIPTION'...which in this case would be 'ODC-IP072-ODC'

If I simply "return xmlDoc" I get the below text. Since all of the elements are named "ns2:StringAttribute", how do I parse through to get the 'WORK_ORDER_DESCRIPTION" value?


<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ValueObjects xmlns="http://www.cybershift.com/wfm3/mcs10/" xmlns:ns2="http://www.cybershift.com/wfm3/5/3/"><valueObject action="ADD_OR_UPDATE" pageNumber="0" pageSize="0"><ns2:StringAttribute name="WORK_ORDER_WORK_ORDER_ID"><ns2:string>W-00000231</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_DESCRIPTION"><ns2:string>ODC-IP072-ODC</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_TYPE"><ns2:string>J</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_SOURCE_SYSTEM"><ns2:string>SAP-WBS</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_CENTRE"><ns2:string>AUTO</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_POS"><ns2:string>AUTO</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_STATUS"><ns2:string>Open</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_UDF_02"><ns2:string>D</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_UDF_11"><ns2:string>2017-03-23 11:41:08</ns2:string></ns2:StringAttribute></valueObject></ValueObjects></soapenv:Body></soapenv:Envelope>

1 ACCEPTED SOLUTION

Try this out and see if it works

	var header = new XMLHelper(xmlDoc);
	var header_obj = header.toObject();
	
	var arr = header_obj["soapenv:Body"]["ValueObjects"]["valueObject"];
	
	for(i in arr){
		
		for(j in arr[i]["ns2:StringAttribute"]){
			
			if(arr[i]["ns2:StringAttribute"][j]["@name"] == "WORK_ORDER_DESCRIPTION")
				gs.log('ABC: ' + arr[i]["ns2:StringAttribute"][j]["ns2:string"]);
		}
	}

View solution in original post

6 REPLIES 6

Joe Wilmoth
ServiceNow Employee
ServiceNow Employee

This should help you: XML Stuff

dvp
Mega Sage
Mega Sage

You can use XMLHelper to convert to object and then do the dot walking

Try this script

var header = new XMLHelper(xmlDoc);
var header_obj = header.toObject();


var arr = header_obj["soapenv:Body"]["ValueObjects"]["valueObject"]["ns2:StringAttribute"];

for(i in arr){
	
	if(arr[i]["@name"] == "WORK_ORDER_DESCRIPTION")
		gs.log(arr[i] + '::' + arr[i]["ns2:string"]);
	
	
}

Patrick Quinlan
Giga Guru

@dvp....

thank you for the response. I've marked it as the correct answer but I do have a follow up question. 

If my xmlDoc has multiple sections of <valueObject> (query returns mutiple results), how would I set through the StringAttributes of each?

Removing the if condition will iterate through all the string attributes