- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2016 07:40 AM
Hi,
For the people who are using Scripted REST API's to post and update data to ServiceNow, how do you handle data validation? Specifically for reference fields? I have found that if you pass in an invalid sys_id, the record is still created with a null value where the sys_id was invalid. This is unexpected behavior, as I would expect an error response of an invalid value.
Am I missing something trivial here or is the developer responsible for handling all data validation?
Thanks!
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2016 07:44 AM
Hi Greg,
For scripted REST API, you are in control. I recommend validating the sys_id before inserting/updating the record.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2016 07:44 AM
Hi Greg,
For scripted REST API, you are in control. I recommend validating the sys_id before inserting/updating the record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-10-2016 07:53 AM
Thanks Chuck!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-20-2018 02:58 PM
Hello Chuck,
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"
}
]
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-21-2018 05:58 AM
System Definition> Relationships is for defining and related list between two tables that don't normally have a reference field or many to many definition. It is not a table we can query to find records from one table to another in a scripted REST APi. I've written the basics of a scripted REST API that looks through the cmdb table for records that match your sys_id specified on the URL as
https://YOUINSTANCENAME.service-now.com/api/YOURSCROPENAME/find_active_cis?sys_id=SOMESYSIDHERE
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var list = [];
var sys_id = request.queryParams.sys_id.toString();
var cmdbGr = new GlideRecord('cmdb');
cmdbGr.addQuery('sys_id', sys_id);
cmdbGr.addQuery('install_status', 'IN', '1,6'); // adjust as needed
cmdbGr.query();
while (cmdbGr.next()) {
var obj = {};
obj.sys_id = cmdbGr.getUniqueValue();
obj.ci_name = cmdbGr.getDisplayValue();
obj.ci_name = cmdbGr.getTableName();
list.push(obj);
}
response.setStatus(200);
response.setBody(list);
})(request, response);