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.

Not able to pass date and time query params in scripted REST API

Murali krishna3
Giga Guru

Hello everyone,

I am trying to pass a date and time value on a glide record for incident table to get incidents that are created after particular time. Here is the code that I have been trying,

var timeDuration;

timeDuration = request.queryParams.duration;

var gr= new GlideRecord("incident");
gr.addQuery("sys_id","incident.sys_id");
gr.addEncodedQuery('sys_created_on>=javascript:gs.dateGenerate(timeDuration)');
gr.query();
while(gr.next()){

}

Note: duration is the query parm that I have created on the API. 

 

I have put a log for "timeDuration" whether the passed value from the REST API explorer is getting to it, it is working fine. But I could some error saying so, 

 

org.mozilla.javascript.EcmaError: "timeDuration" is not defined.
Caused by error in sys_ws_operation.c166b7d3db97a748e312748e0f9619f5.operation_script at line 1

==> 1: gs.dateGenerate(timeDuration)

 

Can anyone help me with it?

4 REPLIES 4

Tony Chatfield1
Kilo Patron

Hi, gs.dateGenerate shows parameter 'timeDuration[i]' but I don't see anywhere where i is set.
So I think you are implying timeDuration is an array, but don't identify which value should be utilized from within the array .

Hi Tony, 

 

That's not an array. It was a mistake. Sorry for misleading. It's just a variable.

dvp
Mega Sage

If input is a single value, try the below script


var timeDuration = request.queryParams.duration; //'2019-02-06,00:00:00';


var query = 'sys_created_on>=' + timeDuration;
var gr= new GlideRecord("incident");
gr.addEncodedQuery(query);
gr.query();

while(gr.next()){

}

Hi,

 

Thanks for the response. It worked for me, but the the response I am getting is being converted to GMT, even though I am in EST.

The record in my system shows a different time from the response. can you help me with it?

 

Thanks,

Murali