Import XML API to Servicenow Alert table

Munny1
Tera Expert

Can someone help me how to import API XML data to ServiceNow table.
I have created rest message  response got the response and scripted rest API i am getting empty response when i tested through rest API explore.

Can someone guide me the sample code


3 REPLIES 3

Rajesh Chopade1
Mega Sage

hi @Munny1 

 

 

Could you please provide your scripted REST API code here for review?

Also, you can first test the external API to ensure it sends you the proper response (you can use a third-party tool such as POSTMAN).

 

 

(function execute() {
  try {
    var restMsg = new RESTMessage('BOJ RSS Feed', 'get');
    var response = restMsg.execute();
    var httpStatus = response.getStatusCode();
    
    if (httpStatus !== 200) {
      gs.error('Failed to fetch RSS feed. HTTP Status: ' + httpStatus);
      return;
    }
    
    var xmlString = response.getBody();
    var xmlDoc = new XMLDocument2();
    xmlDoc.parseXML(xmlString);
    
   
    var items = xmlDoc.getNodes("//item");
    
    for (var i = 0; i < items.length; i++) {
      try {
        var item = items[i];
        
        // Extract fields safely
        var title = xmlDoc.getNodeText("title", item) || 'No Title';
        var guid = xmlDoc.getNodeText("guid", item) || '';
        var pubDate = xmlDoc.getNodeText("pubDate", item) || new GlideDateTime().getDisplayValue();
        var link = xmlDoc.getNodeText("link", item) || '#';
        var description = xmlDoc.getNodeText("description", item) || 'No Description';
        
        // Check for duplicates
        var gr = new GlideRecord('u_regulatory_alerts'); // Replace with your table name
        gr.addQuery('guid', guid);
        gr.query();
        
        if (!gr.hasNext()) {
          gr.initialize();
          gr.title = title.substring(0, 100); // Truncate to avoid field limits
          gr.guid = guid;
          gr.publication_date = this._parseRSSTime(pubDate); // Parse date
          gr.link = link;
          gr.description = description;
          gr.insert();
        }
      } catch (itemErr) {
        gs.error('Error processing item ' + i + ': ' + itemErr.message);
      }
    }
  } catch (e) {
    gs.error('Critical error in BOJ RSS script: ' + e.message);
  }
})();

I am getting the response in rest message 

 

Above script I have used in scripted rest api

The script looks mostly correct, modify the getNodeText() call to include the full XPath or relative path for each child node.

var title = xmlDoc.getNodeText("item/title", item) || 'No Title';
var guid = xmlDoc.getNodeText("item/guid", item) || '';
var pubDate = xmlDoc.getNodeText("item/pubDate", item) || new GlideDateTime().getDisplayValue();
var link = xmlDoc.getNodeText("item/link", item) || '#';
var description = xmlDoc.getNodeText("item/description", item) || 'No Description';