Is it possible to separate the contents of the Query Parameter with commas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2022 09:31 PM
Hi.
We are learning about the Query Parameter of the Scripted API because of your requirements.
This time, we are creating a Query Parameter called "TEST". By design, query parameter values are passed separated by commas.
When I tried it with the Explore REST API, it didn't return anything to the response.
If it's comma separated, do I need to add any processing in the script?
If you know this, please let us know.
Thank you!
Script Rest Resource(GET)
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
// implement resource here
var bodyArray = [];
var query = request.queryParams.TEST;
QueryString ="active=true^stateIN1,2";
var testData = new GlideRecord("incident");
testData.addEncodedQuery(QueryString);
testData.addEncodedQuery(query);
testData.query();
while(testData.next()){
var body = {};
body.number = testData.getValue("number");
body.state = testData.getDisplayValue("state");
body.priority = testData.getDisplayValue("priority");
body.sysID = testData.getDisplayValue("sys_id");
bodyArray.push(body);
}
response.setBody(bodyArray);
})(request, response);
REST API Exploer
Copy query from filter, not comma separated
Response Body
{
"result": [
{
"number": "INC0000020",
"state": "In Progress",
"priority": "5 - Planning",
"sysID": "46edaa6aa9fe198101b9d14ced16619f"
},
{
"number": "INC0000029",
"state": "In Progress",
"priority": "5 - Planning",
"sysID": "46f67787a9fe198101e06dfcf3a78e99"
},
{
"number": "INC0000039",
"state": "New",
"priority": "5 - Planning",
"sysID": "471bfbc7a9fe198101e77a3e10e5d47f"
},
{
"number": "INC0000057",
"state": "New",
"priority": "5 - Planning",
"sysID": "78271e1347c12200e0ef563dbb9a7109"
},
{
"number": "INC0000058",
"state": "New",
"priority": "5 - Planning",
"sysID": "965c9e5347c12200e0ef563dbb9a7156"
},
{
"number": "INC0007002",
"state": "New",
"priority": "4 - Low",
"sysID": "ff4c21c4735123002728660c4cf6a758"
},
{
"number": "INC0008001",
"state": "New",
"priority": "5 - Planning",
"sysID": "0c5f3cece1b12010f877971dea0b1449"
},
{
"number": "INC0008111",
"state": "New",
"priority": "5 - Planning",
"sysID": "a83820b58f723300e7e16c7827bdeed2"
},
{
"number": "INC0008112",
"state": "New",
"priority": "5 - Planning",
"sysID": "552c48888c033300964f4932b03eb092"
},
{
"number": "INC0009009",
"state": "New",
"priority": "4 - Low",
"sysID": "57af7aec73d423002728660c4cf6a71c"
},
{
"number": "INC0010001",
"state": "New",
"priority": "5 - Planning",
"sysID": "f62341638790111006738517cebb35e0"
}
]
}
Comma separated (requirements)
Result
{
"result": []
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2022 09:42 PM
Hi,
priority=4,5 is not a valid query when it gets applied and hence it's not working
the correct one is using the IN operator which would help in forming the correct encoded query
priorityIN4,5
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 09:45 PM
Thank you for your reply. Is it difficult to use a condition stored in a comma-separated list condition as a query parameter?
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2022 10:25 PM
Hi,
you can use path parameters instead and each one of that can have comma separated values
/{query1}/{query2}
INTRODUCTION TO SCRIPTED REST APIS
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
09-19-2022 10:58 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader