How to use Query Parameter of Scripted API

d-aizawa
Kilo Sage

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:
find_real_file.png

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"
    },.........
]
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

View solution in original post

4 REPLIES 4

Chandu Telu
Tera Guru
Tera Guru

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,

Ankur Bawiskar
Tera Patron
Tera Patron

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

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

Hi @Ankur Bawiskar 

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

 

Hitoshi Ozawa
Giga Sage
Giga Sage

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);