- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2016 04:02 PM
Hi all,
I am new to ServiceNow and Scripted REST APIs. I have looked at several resources and have not been successful in getting an answer. I would like my Scripted REST API to return XML. Is this possible? It seems others are only returning XML when using SOAP.
I am able to get the output I want, but not in an appropriate format.
Below is my current Scripted REST Resource Script (Geneva). I want to return all the names of the applications in a table.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var xmldoc = new XMLDocument("<myResponse></myResponse>");
var application = new GlideRecord("cmdb_ci_appl");
application.query();
while (application.next()) {
xmldoc.createElement("application", application.name);
}
return xmldoc.getDocumentElement().toString();
})(request, response);
After editing the script to return XML, below is my response body.
Any help would be very appreciated - thanks in advance.
Hannah
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2016 04:27 AM
Hi Hannah,
I hope below blog will help you find your solution.
Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2016 01:09 PM
Hi Hannah - you might want to take a look at ServiceNow's XMLHelper. In particular, you may be able to use toXMLDoc() or toXMLStr() method to convert the object you're creating from GlideRecords to XML for output. The steps might be
- Build the desired JavaScript object from GlideRecords (may be nested from multiple tables).
- Use toXMLDoc() or toXMLStr() method to convert the final object to XML.
- Output the XML via response.
It may be easier to build the data structure using a JavaScript object and then convert to XML than to manipulate XMLDocument directly.
Please feel free to connect, follow, mark helpful / answer, like, endorse.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-28-2016 04:27 AM
Hi Hannah,
I hope below blog will help you find your solution.
Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-02-2016 12:24 PM
Thank you for your help! I was able to get my desired output by adding these lines to my code:
response.setStatus(200);
response.setContentType('application/xml');
var writer=response.getStreamWriter();
writer.writeString(xmlDocument.getDocumentElement().toString());
Below is my solution that retrieves all application names and sys_ids from the applications table and returns them in XML.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var xmlDocument = new XMLDocument2();
var application = new GlideRecord("cmdb_ci_appl");
var xmlApplicationsElement = xmlDocument.createElement("Applications");
var xmlApplicationElement;
xmlDocument.setCurrentElement(xmlApplicationsElement);
xmlApplicationsElement.setAttribute("timestamp", new Date().toString());
application.query();
while (application.next()) {
xmlApplicationElement = xmlDocument.createElement("Application");
xmlApplicationElement.setAttribute("name", application.name);
xmlApplicationElement.setAttribute("sys_id", application.sys_id);
gs.log("creating XML element for: " + application.name);
}
response.setStatus(200);
response.setContentType('application/xml');
var writer=response.getStreamWriter();
writer.writeString(xmlDocument.getDocumentElement().toString());
})(request, response);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-03-2016 01:15 AM
hannahrobertson great to read that you have cracked it. Good luck with the rest of your endeavours.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2018 02:57 PM
Hello,
I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.
Could you please guide me how to implement this? Sample code would be great.
Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"sys_id": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"sys_id": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}