Scripted REST API - Query Parameters are always arrays?

Chris M3
Tera Guru

This seems odd to me, and does not match what they show as their example on the docs site listed below.

I'm creating a Scripted REST API to simplify interactions with catalog variables.   Currently working on the simple 'Get Variables' request, to get the list of Variables for the RITM.   I added an optional query paramenter 'variables', that can be passed as a comma delimited list of variable names to return if the caller does not need the full variable list.

I got it working, however I was surprised to find that the variables object was an array with one entry, which was a string of the full list.

// I was expecting to write code like this

var responseVariables = request.queryParams.variables.split(',');

// But instead I found I had to write code like this

var responseVariables = request.queryParams.variables[0].split(',');

// The example on the docs site (which admittedly I haven't tried to reproduce looks like this

var queryParams = request.queryParams;

var isActiveQuery = queryParams.active;

var nameQueryVal = queryParams.name;

// However, based off my experience, I believe the code would have to be

var queryParams = request.queryParams;

var isActiveQuery = queryParams.active[0];

var nameQueryVal = queryParams.name[0];

Logging the queryParams object from my call (Using JSUtl) returned the below

Object

variables: Array of 1 elements

[0]: string = ref_requested_for,slt_preferred_name

api: Array of 1 elements

[0]: string = api

Is there something I'm missing?   I am developing this in Istanbul

RESTAPIRequest - queryParams

Ugh.. Accidently marked the Assumed Answered.. Obviously no answers yet.

2 REPLIES 2

SRose
ServiceNow Employee
ServiceNow Employee

Hi Chris,



You are correct, you'll need to have the [0] as the params are arrays.



If you use this


var isActiveQuery = queryParams.active;


then outputting that will give you an error like this, saying its an array. java.lang.ClassCastException: org.mozilla.javascript.NativeArray cannot be cast to java.lang.String



var isActiveQuery = queryParams.active[0];



Will work.





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