Pushing update sets using API

SNow35
Giga Guru

We need to fetch the XML from a location and then preview and commit using API, has anyone done this?

 

Thanks in Advance.

5 REPLIES 5

sourav1999
Mega Guru

Yes, you can fetch XML from a location and then preview and commit using ServiceNow's REST API. Here are the steps:

 

1. Fetch XML from a location:

- Use the REST API to fetch the XML from a location. You can use the GET method to fetch the XML.

- For example, you can use the following code to fetch the XML:

javascript

var restMessage = new sn_ws.RESTMessageV2();

restMessage.setHttpMethod('get');

restMessage.setEndpoint('your_endpoint_url');

var response = restMessage.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

 

2. Preview the XML:

- After fetching the XML, you can preview it. You can use the gs.info() method to log the XML for preview.

- For example, you can use the following code to preview the XML: javascript gs.info(responseBody);

 

3. Commit the XML:

- After previewing the XML, you can commit it to a table in ServiceNow.

- You can use the parseXML() method to parse the XML and then use the insert() method to commit the data to a table.

- For example, you can use the following code to commit the XML:

javascript

var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(responseBody);

var gr = new GlideRecord('your_table_name');

gr.initialize();

gr.column1 = xmlDoc.getNodeText('//node1');

gr.column2 = xmlDoc.getNodeText('//node2');

gr.insert(); - Replace 'your_table_name', 'column1', 'column2', '//node1', and '//node2' with your actual table name, column names, and XML node paths.

 

Please note that you need to have the appropriate roles and permissions to use the REST API and to commit data to a table.

This answer as well as the next one clearly looks like someone has just asked ChatGPT and pasted the response here without checking if it makes sense or not. ( I can tell from the "javascript" keywords before the code which ChatGPT uses.)
Unfortunately ChatGPT is "hallucinating" often when it comes to ServiceNow: It believes it's answers sound "fitting" but doesn't really *understand*  how SN works.
In this case
* the Requester probably wanted to upload the files from a local location and not some http-url.
* a preview for an update set is not equal to just using gs.info to "look" at it.
* committing is not "committing it to a table"  like in an ordinary Database.
@sourav1999: Please do not just post ChatGPT responses here: The Original requestor could have easily done this himself and he would have known to take the answer with caution.
At a minimum, please clearly mark your response as "Source: ChatGPT" !
Thank you!

sumanta pal
Kilo Guru

Yes, you can fetch XML from a location and then preview and commit using ServiceNow's API. Here are the steps: 1. **Fetch XML from a location:** - Use the GlideHTTPRequest class to fetch the XML from a location. Here is a sample code: javascript var gr = new GlideHTTPRequest('http://example.com/xml'); var response = gr.get(); var responseBody = response.getBody(); - The responseBody variable now contains the XML data. 2. **Preview and commit using API:** - ServiceNow provides the Import Set API to preview and commit data. You can use the sys_import_set and sys_import_set_row tables to manage the import process. - Here is a sample code to create an import set: javascript var importSet = new GlideRecord('sys_import_set'); importSet.initialize(); importSet.setValue('name', 'My Import Set'); var importSetSysId = importSet.insert(); - Then, you can add rows to the import set using the sys_import_set_row table: javascript var importSetRow = new GlideRecord('sys_import_set_row'); importSetRow.initialize(); importSetRow.setValue('sys_import_set', importSetSysId); importSetRow.setValue('u_data', responseBody); importSetRow.insert(); - After adding all rows, you can run the import using the startImport function: javascript var importSetRunner = new GlideImportSetRun(importSetSysId); importSetRunner.startImport(); Please note that you need to replace 'http://example.com/xml' with the actual URL of the XML file, and 'My Import Set' with the actual name of your import set. Also, you need to parse the XML data and add it to the import set row by row.