The CreatorCon Call for Content is officially open! Get started here.

how to parse xml response from netsuite

Mahesh120
Tera Expert

Hi Team,

Unable to parse received xml response using SOAP Request.

Please find the attached xml response record.

var xmlDoc = new XMLDocument(respBody);

var nodes = xmlDoc.getNodes("//searchRowList/*");

var theStr = '';

for(var i = 0 ; i < nodes.length ; i++){

theStr = nodes.item(i).getTextContent()+'\n';

gs.print(theStr);

}

And the response is as follows

*** Script: Record Internal IDWalt.JohnseyInput textSymboticCorey.Ebbers. 
*** Script: Record Internal IDWalt.JohnseyInput textSymbotic.Corey.Ebbers2014-10-22T07:00:00.000-07:00Corey.Ebbers2014-10-22T07:30:00.000-07:00
*** Script: Record Internal IDWalt.JohnseyInput textSymboticCaren.ShwadCorey.Ebbers.
*** Script: Record Internal IDWalt.JohnseyInput textSymbotic.Corey.Ebbers2014-12-01T06:00:00.000-08:00Caren.Shwad2014-12-01T06:59:59.000-08:00
*** Script: Record Internal IDWalt.JohnseyInput textSymbotic.Corey.Ebbers2014-12-01T06:00:00.000-08:00Caren.Shwad2014-12-02T06:59:59.000-08:00
*** Script: Record Internal IDWalt.JohnseyInput textSymboticGregg.W. Steinhafelrewgweryhedgs asjkhashlkfCorey.Ebbers.

But interested in to get out put as follows

Record Internal ID = 1

custrecord_erc_primarycontactid = Corey.Ebbers

custrecord_erc_secondarycontactid = Walt.Johnsey

Any help on this would be much appreciated.

--Mahesh.

9 REPLIES 9

Mahesh120
Tera Expert

XML file attached.


Always - my suggestion would be to use XMLHelper for parsing any XML.



XMLHelper - ServiceNow Wiki


var xh = new XMLHelper(respBody);


var obj = xh.toObject();



var results = obj['soapenv:Body']['searchResponse']['platformCore:searchResult']['platformCore:searchRowList']['platformCore:searchRow']['setupCustom:basic']['platformCommon:customFieldList']['platformCore:customField']


for(var i in results){


for(var key in results[i]){


gs.log(results[i][key]);


}



}


If I were you, and I am dealing with huge XMLs like the one you attached, I would *never* use XPath.




Explanation


==========



What XMLHelper does is convert the given string into JSON Object. So the corresponding JSON for your XML string is below:



{"@xmlns:soapenv":"http://schemas.xmlsoap.org/soap/envelope/","@xmlns:xsd":"http://www.w3.org/2001/XMLSchema","@xmlns:xsi":"http://www.w3.org/2001/XMLSchema-instance","soapenv:Body":{"searchResponse":{"@xmlns":"urn:messages_2014_2.platform.webservices.netsuite.com","platformCore:searchResult":{"@xmlns:platformCore":"urn:core_2014_2.platform.webservices.netsuite.com","platformCore:pageIndex":"1","platformCore:pageSize":"1000","platformCore:searchId":"WEBSERVICES_3434368_0204201514174360161459818983_2f2d13da6d6","platformCore:searchRowList":{"platformCore:searchRow":{"@xmlns:setupCustom":"urn:customization_2014_2.setup.webservices.netsuite.com","@xsi:type":"setupCustom:CustomRecordSearchRow","setupCustom:basic":{"@xmlns:platformCommon":"urn:common_2014_2.platform.webservices.netsuite.com","platformCommon:customFieldList":{"platformCore:customField":[{"@internalId":"905","@scriptId":"custrecord_erc_changetype","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"2","@typeId":"79"}},{"@internalId":"904","@scriptId":"custrecord_erc_2ndcontact","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"3787","@typeId":"-6"}},{"@internalId":"927","@scriptId":"custrecord_erc_secondarycontactid","@xsi:type":"platformCore:SearchColumnStringCustomField","platformCore:searchValue":"Walt.Johnsey"},{"@internalId":"908","@scriptId":"custrecord_erc_shortdesc","@xsi:type":"platformCore:SearchColumnStringCustomField","platformCore:searchValue":"Input text"},{"@internalId":"918","@scriptId":"custrecord_erc_contacted","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"2","@typeId":"82"}},{"@internalId":"907","@scriptId":"custrecord_erc_vendorother","@xsi:type":"platformCore:SearchColumnStringCustomField","platformCore:searchValue":"Symbotic"},{"@internalId":"917","@scriptId":"custrecord_erc_implemduration","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"1","@typeId":"84"}},{"@internalId":"910","@scriptId":"custrecord_erc_whichendpoints","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"1","@typeId":"81"}},{"@internalId":"914","@scriptId":"custrecord_erc_knowimplementationdate","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"2","@typeId":"82"}},{"@internalId":"926","@scriptId":"custrecord_erc_primarycontactid","@xsi:type":"platformCore:SearchColumnStringCustomField","platformCore:searchValue":"Corey.Ebbers"},{"@internalId":"902","@scriptId":"custrecord_erc_scheduletype","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"1","@typeId":"78"}},{"@internalId":"909","@scriptId":"custrecord_erc_changeendpoints","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"1","@typeId":"80"}},{"@internalId":"911","@scriptId":"custrecord_erc_outrage_degration","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"2","@typeId":"82"}},{"@internalId":"928","@scriptId":"custrecord_erc_whoiscontactid","@xsi:type":"platformCore:SearchColumnStringCustomField","platformCore:searchValue":"."},{"@internalId":"913","@scriptId":"custrecord_erc_enduserlocation","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"3","@typeId":"83"}},{"@internalId":"903","@scriptId":"custrecord_erc_primarycontact","@xsi:type":"platformCore:SearchColumnSelectCustomField","platformCore:searchValue":{"@internalId":"3786","@typeId":"-6"}}]},"platformCommon:internalId":{"platformCore:customLabel":"Record Internal ID","platformCore:searchValue":{"@internalId":"1"}},"platformCommon:recType":{"@internalId":"77"}}}},"platformCore:status":{"@isSuccess":"true"},"platformCore:totalPages":"1","platformCore:totalRecords":"6"}}},"soapenv:Header":{"platformMsgs:documentInfo":{"@xmlns:platformMsgs":"urn:messages_2014_2.platform.webservices.netsuite.com","platformMsgs:nsId":"WEBSERVICES_3434368_0204201514174360161459818983_2f2d13da6d6"}}}



Then all we have to do is parse this JSON object which is breeze and gives you better Error handling capability than XPath, which I think is *primitive*.


Hello Abhiram,



I have tried this, when i tried to execute this in background script it is giving an error.



[0:00:00.398] Script completed: script



Javascript compiler exception: The undefined value has no properties. (<refname>; line 56) in:

Line 56 represents


var results = obj['soapenv:Body']['searchResponse']['platformCore:searchResult']['platformCore:searchRowList']['platformCore:searchRow']['setupCustom:basic']['platformCommon:customFieldList']['platformCore:customField'];



Even I have placed this line in business rule to verify any syntax errors, it is giving a warning message


WARNING at line 56: ['searchResponse'] is better written in dot notation.




Regards,


Mahesh.