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.

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

Patrick Quinlan
Giga Guru

So, if my xmlDoc contains multiple <valueObject> which contain Multiple <ns2:StringAttribute> each, is there a way to iterate through all of them? For instance, in this specific example, my SOAP response returns all Work Items for a particular Work Order. I want to compile a list of the Work Item ID ("WORK_ORDER_ITEM_WORK_ITEM_ID"). Removing the if condition returns nothing.

<?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 pageSize="0" pageNumber="0" action="ADD_OR_UPDATE"><ns2:StringAttribute name="WORK_ORDER_ITEM_WORK_ORDER_ID"><ns2:string>W-00000231</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_WORK_ITEM_ID"><ns2:string>W-00000234</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_DESCRIPTION"><ns2:string>ODC-IP072.01.40.01-AAP FABRICATION AND P</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_STATUS"><ns2:string>Open</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_UDF_01"><ns2:string>XXX-8888-8888-0000</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_HAS_INFO_CODE"><ns2:string>false</ns2:string></ns2:StringAttribute></valueObject><valueObject pageSize="0" pageNumber="0" action="ADD_OR_UPDATE"><ns2:StringAttribute name="WORK_ORDER_ITEM_WORK_ORDER_ID"><ns2:string>W-00000231</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_WORK_ITEM_ID"><ns2:string>W-00000235</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_DESCRIPTION"><ns2:string>ODC-IP072.01.40.03-HYDRAUL&amp;SUBASSY&amp;LINE</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_STATUS"><ns2:string>Open</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_UDF_01"><ns2:string>XXX-8888-8888-0000</ns2:string></ns2:StringAttribute><ns2:StringAttribute name="WORK_ORDER_ITEM_HAS_INFO_CODE"><ns2:string>false</ns2:string></ns2:StringAttribute></valueObject></ValueObjects></soapenv:Body></soapenv:Envelope>

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"]);
		}
	}