Scripted REST API with XML Response

hannahrobertson
Kilo Contributor

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.

find_real_file.png


Any help would be very appreciated - thanks in advance.

Hannah

1 ACCEPTED SOLUTION

Deepa Srivastav
Kilo Sage

Hi Hannah,



I hope below blog will help you find your solution.



Scripted REST APIs - Part 2



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


View solution in original post

11 REPLIES 11

drjohnchun
Tera Guru

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



  1. Build the desired JavaScript object from GlideRecords (may be nested from multiple tables).
  2. Use toXMLDoc() or toXMLStr() method to convert the final object to XML.
  3. 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.


John Chun, PhD PMP see John's LinkedIn profile


visit snowaid


Deepa Srivastav
Kilo Sage

Hi Hannah,



I hope below blog will help you find your solution.



Scripted REST APIs - Part 2



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


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);


hannahrobertson great to read that you have cracked it. Good luck with the rest of your endeavours.


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