- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 06:18 AM
Hi,
I have a scripted REST End Point which is getting the following XML content:
<?xml version="1.0" encoding="UTF-8"?> <company> <employee> <id>10</id> <firstname>Tom</firstname> <lastname>Cruise</lastname> <test>test1</test> <test>test3</test> </employee> <employee> <id>20</id> <firstname>Jennifer</firstname> <lastname>Aniston</lastname> <test>test11</test> <test>test33</test> </employee> </company>
i need to iterate over each "<employee>" node and insert/udpate the record in our instance. Can someone please provide help with the code im not very well versed with Glide XML Utils/Document.
P.S the REST service is in Global scope.
johnandersen please help
Regards,
JS
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 08:03 PM
the issue was that calling `request.body.dataString` flushes it from the memory, i had something like
gs.log("[REST REQ] "+request.body.dataString);
var xmlDoc = new SMLDocument(request.body.dataString); //this was empty because i called request.body.dataString already once in the log statement
so i cached the `request.body.dataString` like
var req = request.body.dataString;
and the rest of the code kicked in just fine.
Thanks all for providing very useful information
Regards,
JS

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 06:25 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 06:27 AM
John,
I would refer you to the XMLHelper documentation on the wiki. In the example code below xmlString would be the XML content you posted above.
var helper = new XMLHelper(xmlString);
var obj = helper.toObject();
Obj will be a regular Javascript object you can manipulate or iterate over as you wish
Links
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 07:05 AM
Hi John,
Following links should be helpful:
http://wiki.servicenow.com/index.php?title=XMLDocument_Script_Object#gsc.tab=0
Also below is the sample script which you can execute in background to parse all the values and insert into target table, which can be replicated at the place where you are getting the response.
Script:
var xmlString = '<?xml version="1.0" encoding="UTF-8"?> <company> <employee> <id>10</id> <firstname>Tom</firstname> <lastname>Cruise</lastname> <test>test1</test> <test>test3</test> </employee> <employee> <id>20</id> <firstname>Jennifer</firstname> <lastname>Aniston</lastname> <test>test11</test> <test>test33</test> </employee> </company>';
var xmldoc = new XMLDocument(xmlString);
var employeeLength = xmldoc.getNodes("/company/employee").getLength();
var tableGr = new GlideRecord('<tableName>'); // replace with your tableName
var idValue = xmldoc.getNodeText("/company/employee[1]/id");
for(var k=1; k<=employeeLength; k++){
// replace corresponding fields on left hand side of the script lines below to actual columns in that table
tableGr.initialize();
tableGr.<idField> = xmldoc.getNodeText("/company/employee["+k+"]/id");
tableGr.<firstnameField> = xmldoc.getNodeText("/company/employee["+k+"]/firstname");
tableGr.<lastnameField> = xmldoc.getNodeText("/company/employee["+k+"]/lastname");
tableGr.<testField> = xmldoc.getNodeText("/company/employee["+k+"]/test");
tableGr.insert();
}
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-07-2017 08:03 PM
the issue was that calling `request.body.dataString` flushes it from the memory, i had something like
gs.log("[REST REQ] "+request.body.dataString);
var xmlDoc = new SMLDocument(request.body.dataString); //this was empty because i called request.body.dataString already once in the log statement
so i cached the `request.body.dataString` like
var req = request.body.dataString;
and the rest of the code kicked in just fine.
Thanks all for providing very useful information
Regards,
JS