Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Thijs Daemen
Mega Guru

Hi Ravish,



Take a look at Table API Server-Side JavaScript Examples - ServiceNow Wiki for some actual examples.



var request =new GlideHTTPRequest


    ('https://'+instanceName+'.service-now.com/api/now/table/sys_user_grmember.do');


request.setBasicAuth(authenticatingUsername,authenticatingPassword);


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



var response = request.get();



gs.log(response.getStatusCode());


gs.log(response.getBody());




You can also use the new api's in Fuji for scoped applications (Please note: The APIs below are for scoped applications and may behave differently in the global scope), follow these link for the API docs:


https://developer.servicenow.com/app.do#!/api_doc?to=class__restmessagev2


https://developer.servicenow.com/app.do#!/api_doc?to=class__restresponsev2



Also, take a look at REST API Explorer - ServiceNow Wiki for the REST API explorer, which is a very nice tool to discover more about REST in combination with ServiceNow.


Hello Thijs,



If you check line 4 of my script, i am applying a specific query. I want to do the same so that i do not fetch all the data. Can i apply a specific query in GlideHTTPRequest?


Not sure about GlideHTTPRequest, i guess you could put it in the URL.



The rest API explorer gives us the following:


Screen Shot 2015-08-23 at 20.17.45.png



Leads to this URL https://devxxx.service-now.com/api/now/table/sys_user_grmember?sysparm_limit=10&sysparm_query=active=true



The following ServiceNow script to replicate:


var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://devxxx.service-now.com/api/now/table/sys_user_grmember?sysparm_limit=10&sysparm_query=active...


request.setHttpMethod('GET');



//Eg. UserName="admin", Password="admin" for this code sample.


var user = 'admin';


var password = 'admin';



request.setBasicAuth(user,password);


request.setRequestHeader("Accept","application/json");




var response = request.execute();


gs.log(response.getBody());


Some additional info from the wiki (fuji release): http://wiki.servicenow.com/index.php?title=Scripting_Outbound_REST#gsc.tab=0