- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 02:07 AM
Hi.
I tried to write the Scripted API as below to understand the specification of Query Parameter, but I feel that the query is not normal.
If there is something wrong, please let me know.
Script REST Resource(GET):
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var testData = new GlideRecord("incident");
var bodyArray = [];
testData.request.queryParms.TEST;
testData.query();
while(testData.next()){
var body ={};
body.number = testData.getValue("number");
body.priority = testData.getDisplayValue("priority");
body.sysID = testData.getDisplayValue("sys_id");
bodyArray.push(body);
}
response.setBody(bodyArray);
})(request, response);
QueryParameter Associations:
Response Body:(It should be a parameter that extracts only priority 4 and 5, but for some reason other things have been added.)
{
"result": [
{
"number": "INC0000060",
"priority": "3 - Moderate",
"sysID": "1c741bd70b2322007518478d83673af3"
},
{
"number": "INC0009002",
"priority": "3 - Moderate",
"sysID": "1c832706732023002728660c4cf6a7b9"
},
{
"number": "INC0000009",
"priority": "1 - Critical",
"sysID": "46b66a40a9fe198101f243dfbc79033d"
},
{
"number": "INC0000010",
"priority": "4 - Low",
"sysID": "46b9490da9fe1981003c938dab89bda3"
},.........
]
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 02:22 AM
Hi,
since you are forming the query in query parameter; update as this and test once
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var bodyArray = [];
var query = request.queryParams.TEST;
var testData = new GlideRecord("incident");
testData.addEncodedQuery(query);
testData.query();
while(testData.next()){
var body = {};
body.number = testData.getValue("number");
body.priority = testData.getDisplayValue("priority");
body.sysID = testData.getDisplayValue("sys_id");
bodyArray.push(body);
}
response.setBody(bodyArray);
})(request, response);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 02:10 AM
Hi,
Looks like there is no addQuery for P1 and P2 only
var bodyArray = [];
var testData = new GlideRecord("incident");
testData.addEncodedQuery("active=true^priorityIN1,2") // If P1 and P2 is static
testData.addEncodedQuery( Query)// You can replace with your query
//testData.request.queryParms.TEST;
testData.query();
Thanks
Chandu Telu
Please Mark ✅ Correct/helpful, if applicable,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 02:22 AM
Hi,
since you are forming the query in query parameter; update as this and test once
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var bodyArray = [];
var query = request.queryParams.TEST;
var testData = new GlideRecord("incident");
testData.addEncodedQuery(query);
testData.query();
while(testData.next()){
var body = {};
body.number = testData.getValue("number");
body.priority = testData.getDisplayValue("priority");
body.sysID = testData.getDisplayValue("sys_id");
bodyArray.push(body);
}
response.setBody(bodyArray);
})(request, response);
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-14-2025 09:25 AM
How to add a time filter in queryparameter?
My requirement is Get Related Records by CI and Date.
Adding an Integer variable to the endpoint, and we can give us an Integer 1-30, and we would return tickets created over that many days.
TIA
Hiranmayee

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 03:26 AM
Hi,
Ankur's script should work.
The problem is with "testData.request.queryParms.TEST;". There's no ".request" method in GlideRecord.
That's why the custom is to prefix GlideRecord variable with "gr" so it'll be more apparent that the variable is a GlideRecord.
var grIncident= new GlideRecord("incident");
grIncident.addEncodedQuery(query);