Compare "Last Scanned" and "Install Date" on cmdb_sam_sw_install table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 06:06 AM
Hello everyone!
I am trying to create a business rule that compares the "last scanned" and "install date" fields, whatever records that are found to be 90 days older than the newest date in the install column, those should be deleted.
Script that i started:
var gr = new GlideRecord('cmdb_sam_sw_install');
gr.query();
while(gr.next()) {
var gdt1 = new GlideDateTime("last_scanned");
var gdt2 = new GlideDateTime("install_date");
var dur = new GlideDuration();
var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
gs.print(dur.getDisplayValue());
}
Is it possible to compare a date field with a date/time field? If so, is there another thread in the community i can reference?
Any information is appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 06:30 AM
Hello Othmos,
Yes, it is possible. You can make use of GlideDateTime and GlideDate either you can convert date into date time or you can convert date time into date.
Example:
date to date time :
var gDate = new GlideDate(); gDate.setValue('2015-01-01'); gs.info(gDate); var gDT = new GlideDateTime(gDate); gs.info(gDT);
OUTPUT:
2015-01-01 2015-01-01 00:00:00
OR:
date time to date:
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.print(gdt.getDate());
Output
2011-08-31
Check out the llink for reference.
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 06:32 AM
Thanks for the reply Abhishek,
One more question, i should query my target table first, correct?
Like this:
var gr = new GlideRecord('cmdb_sam_sw_install');
gr.query();
while(gr.next()) {
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 06:34 AM
1.Yes. you need to query table first.
2. Pass date to glidedatetime
3. then you can compare it
https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GDT-getDate
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-01-2019 06:35 AM
Sample code for finding difference between 2 dates. You can make changes as per your req.
var grTask = new GlideRecord('sc_req_item');
grTask.addQuery('sys_id','5eec52291b3e3300364d32a3cc4bcb08');
grTask.query();
if(grTask.next()){
gs.print('Number'+grTask.number+"|| Created ON: "+grTask.due_date);
var gdt1 = new GlideDateTime(grTask.due_date);
gs.print("CreatedDate: "+gdt1.getDate());
var gdt = new GlideDateTime('2019-07-22 03:56:25');
gs.print("GDT:"+gdt.getDate());
var startDate = gdt1.getDate(); // Replace u_start_date with your field name of Start Date
var currentDate = gdt.getDate();
var diffSeconds = gs.dateDiff(currentDate.getDisplayValue(),startDate.getDisplayValue(), true);
var daysIn = (diffSeconds/(60*60*24));
gs.print(daysIn );
}
Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade
Abhishek Gardade