Hi All,
dynamic way to fetch Incident & Change records from task_ci table created within a specific range of days (1 to 30), based on a user-defined days_ago query parameter through scripted Rest api.
Adding an Integer variable to the endpoint, and you can give us an Integer 1-30, and we would return tickets created over that many days. If you give us a number over 30, we'll return the last 30 days' worth of tickets.
It's throwing an error in below highlighted line.
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
//getting the request data
var ciName = request.pathParams.ci_Name;
var query = request.queryParams;
var daysAgo = query.daysAgo;
var today = new GlideDateTime();
today.addDaysUTC(-daysAgo); // Subtract 'daysAgo' from today to get the start date
var startDate = today.getValue(); // This is the date from which we'll fetch records
var endDate = new GlideDateTime().getValue(); // Current time for the end date
var result = [];
var count = 0; //to track count of response body
var recList = []; //records returned by gliderecord query
// Create a GlideRecord for task_ci to get the related tasks by CI
var grRec = new GlideRecord('task_ci');
grRec.addEncodedQuery('task.sys_class_name=change_request^ORtask.sys_class_name=incident^ORtask.sys_class_name=sc_task');
grRec.addQuery('ci_item.name',ciName);
grRec.addQuery('sys_created_on', '>=', startDate);
grRec.addQuery('sys_created_on', '<=', endDate);
grRec.query();
// Iterate through task_ci records and fetch relevant task details
while (grRec.next()) {
var body = {};
// Depending on the task_type, fetch the respective record (Incident, Change, ScTask)
if (grRec.task.sys_class_name == 'incident') {
var inc = new GlideRecord('incident');
if (inc.get(grRec.task)) {
body.Number = inc.number.toString();
body.State = inc.state.getDisplayValue().toString();
body.ShortDescription = inc.short_description.toString();
body.Description = inc.description.toString();
body.Opened = inc.opened_at.toString();
body.Closed = inc.closed_at.toString();
body.URL = "https://" + gs.getProperty('instance_name') + ".service-now.com/incident.do?sys_id=" + inc.sys_id.toString();
}
}
result.push(body);
response.setBody(result);
})(request, response);
Kindly help for this.
Thanks