To fetch incident, change Records created within a specific range of days (1 to 30)

Hiranmayee Moha
Tera Expert

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
 
 
 
 
 
 
 
 
 
5 REPLIES 5

Sanjay191
Tera Sage

Hello @Hiranmayee Moha 

Please use the below Logic i performed the small changes in query 


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');
var thirtyTwoDaysAgo = new GlideDateTime();
thirtyTwoDaysAgo.addDaysUTC(-32);// it takes the records from current date to last 32 days
taskGR.addEncodedQuery('task.sys_class_name=change_request^ORtask.sys_class_name=incident^ORtask.sys_class_name=sc_task');
taskGR.addQuery('sys_created_on', '>=', thirtyTwoDaysAgo);
taskGR.query();
grRec.addQuery('ci_item.name', ciName);
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);

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

Hi @Sanjay191 

 

Sorry for late response.

 

Could you please explain why we are querying 32 days not 30 days (My requirement range is 1--30)?

 

thirtyTwoDaysAgo.addDaysUTC(-32);// it takes the records from current date to last 32 days
taskGR.addEncodedQuery('task.sys_class_name=change_request^ORtask.sys_class_name=incident^ORtask.sys_class_name=sc_task');
taskGR.addQuery('sys_created_on''>=', thirtyTwoDaysAgo);
 
 
Thanks
Hiranmayee

Hello @Hiranmayee Moha 

You can put according to your requirement I just give you example or logic .Just update the values whatever you want like last 30 days, 7 days etc 

Thanks @Sanjay191