Is it possible to separate the contents of the Query Parameter with commas?

d-aizawa
Kilo Sage

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 separatedfind_real_file.png

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)

find_real_file.png

Result

{
  "result": []
}

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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!

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@d-aizawa 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader