How to query data using GlideHTTPRequest?

Ravish Shetty
Tera Guru

Hello,

With GlideHTTPRequest, i can delete, update and insert data.

However there is this one use case where i want to fetch some data by querying it. In the documentation i did not find anything for that.

Here is the code what i use today using john andersens RESTMessageScripted. I want to use GlideHTTPRequest instead of RESTMessageScripted.


var r = new RESTMessageScripted("post", "https://"+instanceName+".service-now.com/sys_user_grmember.do");

r.addHeader("Content-Type", "application/json");

r.addRequestParameter("JSONv2", "true");

r.addRequestParameter("sysparm_query", 'group='+group+'^user='+user);

r.setBasicAuth(authenticatingUsername, authenticatingPassword);

response = r.execute();

var jsonData = new JSON().decode(response.getBody().toString());

if(jsonData.records.length == 0){

return true;

} else {

return false;

}

Edit: As shown in Line 4 of the script, i am applying a specific query. I want to do the same with GlideHTTPRequest so that i do not fetch all the data. Can i apply a specific query using GlideHTTPRequest?

1 ACCEPTED SOLUTION

Better yet, use %5E instead of ^ and it will work.



You can also use request.addParameter('sysparm_query', 'active=true%5Enumber=xxxxxxx') to set the query.


View solution in original post

16 REPLIES 16

Better yet, use %5E instead of ^ and it will work.



You can also use request.addParameter('sysparm_query', 'active=true%5Enumber=xxxxxxx') to set the query.


This is what i used and it worked. Thanks.



var request = new GlideHTTPRequest('https://XXXXXXXXXXXX.service-now.com/api/now/table/incident');


request.setBasicAuth("XXXXXXXXXXXX", "XXXXXXXXXXXX");


request.addHeader('Accept', 'application/json');


request.addParameter('sysparm_query','active=true%5Enumber=XXXXXXXXXXXX');


var response = request.get();



var jsonData = new JSON().decode(response.getBody().toString());



gs.log(response.getStatusCode());


gs.log(response.getBody());


gs.log(jsonData.result[0].number);


Hey Ravio,



Why do you want to use GlideHTTPRequest instead of RESTMessageScripted?



Thanks,


Bryan


It is a 3rd party script and won't be supported by ServiceNow if things don't work.


Thijs Daemen
Mega Guru

Retrieving a specific record from the table can be done with the following url syntax



GET https://devxxx.service-now.com/api/now/table/{tableName}/{sys_id}



So you wouldn't need to specify the number in the sysparm query.