Filtering records on a 24 hour period starting from 6am

Ahmad6
Giga Expert

Hi,

We have a security log I am trying to implement into ServiceNow, and the log typically starts/ends at 6am. Using the preset Today/Yesterday fields will filter the results based on 12am until now, does anyone have a clue how I would go about implementing this?

We can't really use relative 24 hours because up until 6am it will always be showing noise from the day before, and we can't use "today after 6am" because between 12am and 6am the following day there will be no records visible. The closest I could think of representing this correctly would be "Today after 6am, if the current time is not before 6am, otherwise Yesterday after 6am and today before 6am".

Assuming this could even be accurately represented in an encoded query string, can you put encoded query strings in the application menu links?

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

So the solution would need to have 2 parts.


  • First, you would need a script part that can act as advanced reference qualifier or as custom filter for your report.


Create a script include and add the function such as below (I named the script as CustomLogsReport)



function CustomLogsReport()


{


  var sysIDs = [];


  var gr = new GlideRecord('incident'); gr.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate(gs.beginningOfYesterday().slice(0,10),'06:00:00')@javascript:gs.dateGenerate(gs.beginningOfToday(),'06:00:00') ");


  gr.query();


  while (gr.next()) {


  sysIDs.push(gr.getValue('sys_id'));


  }


  return sysIDs.toString();


}



  • Now call the script in your report or list view of table.

6-13-2017 12-58-52 PM.png


View solution in original post

9 REPLIES 9

Kalaiarasan Pus
Giga Sage

So the solution would need to have 2 parts.


  • First, you would need a script part that can act as advanced reference qualifier or as custom filter for your report.


Create a script include and add the function such as below (I named the script as CustomLogsReport)



function CustomLogsReport()


{


  var sysIDs = [];


  var gr = new GlideRecord('incident'); gr.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate(gs.beginningOfYesterday().slice(0,10),'06:00:00')@javascript:gs.dateGenerate(gs.beginningOfToday(),'06:00:00') ");


  gr.query();


  while (gr.next()) {


  sysIDs.push(gr.getValue('sys_id'));


  }


  return sysIDs.toString();


}



  • Now call the script in your report or list view of table.

6-13-2017 12-58-52 PM.png


Hi Kalai,


Thanks for your help with this. I've tried to follow your instructions but the "sys_id" being added to the filter is "undefined" so I think there is an issue with how the function is returning the list.



in my filter I'm using sys_id is one of javascript:CustomLogsReport.SecurityLogFilter() and this is the script include;



var CustomLogsReport = Class.create();


CustomLogsReport.prototype = {


  initialize: function() {


  },



  SecurityLogFilter : function(){



  var sysIDs = [];


  var gr = new GlideRecord('u_security_log'); gr.addEncodedQuery("sys_created_onBETWEENjavascript:gs.dateGenerate(gs.beginningOfYesterday().slice(0,10),'06:00:00')@javascript:gs.dateGenerate(gs.beginningOfToday(),'06:00:00') ");


  gr.query();


  while (gr.next()) {


  sysIDs.push(gr.getValue('sys_id'));


  }


  return sysIDs.toString();



  },


  type: 'CustomLogsReport'


};




I've tried to return an actual sys_ID instead of the list but it still appears as "undefined" in the breadcrumbs



find_real_file.png


Make the script include client callable by selecting the client callable checkbox.


Hi Kalai,



I've already made it client callable. Before I had checked that box the breadcrumb was saying "sys_id in null", after checking it the value is undefined.