Compare "Last Scanned" and "Install Date" on cmdb_sam_sw_install table

othomas1
Kilo Guru

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.

 

find_real_file.png

 

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!

26 REPLIES 26

I used this code but got an error:

*****************************************

var sftw = new GlideRecord('cmdb_sam_sw_install');
//sftw.addQuery('sys_id','5eec52291b3e3300364d32a3cc4bcb08');
sftw.query();

if(sftw.next()){

gs.print('Number'+ sftw.number+"|| Created ON: "+ sftw.due_date);

var gdt1 = new GlideDateTime(sftw.isntall_date);

gs.print("CreatedDate: "+gdt1.getDate());

var gdt = new GlideDateTime('sftw.last_scanned');

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 diffDays = gs.dateDiff(currentDate.getDisplayValue(),startDate.getDisplayValue(), true);
var daysIn = (diffDays/(60*60*24));

gs.print(daysIn );
}

 

Output

**************************

*** Script: Numberundefined|| Created ON: undefined
*** Script: CreatedDate: null
*** Script: GDT:null
Evaluator: org.mozilla.javascript.EcmaError: Cannot convert null to an object. Caused by error in script at line 23 20: var startDate = gdt1.getDate(); // Replace u_start_date with your field name of Start Date 21: var currentDate = gdt.getDate(); 22: ==> 23: var diffDays = gs.dateDiff(currentDate.getDisplayValue(),startDate.getDisplayValue(), true); 24: var daysIn = (diffDays/(60*60*24)); 25: 26: gs.print(daysIn );

Its working fine 

1.remove '' from like below.

 var gdt = new GlideDateTime(sftw.last_scanned);

Thanks,

Abhishek

Thank you,
Abhishek Gardade

 I have tested your the code after modifying above line and its working fine. here is the proof

var sftw = new GlideRecord('cmdb_ci_computer');
sftw.addQuery('sys_id','b4fd7c8437201000deeabfc8bcbe5dc1');
sftw.query();
if(sftw.next()){

gs.print('Number'+ sftw.name+" || Created ON: "+ sftw.sys_created_on);

var gdt1 = new GlideDateTime(sftw.sys_created_on);

gs.print("Created Date: "+gdt1.getDate());

var gdt = new GlideDateTime(sftw.sys_updated_on);

gs.print("updated:"+gdt.getDate());

var startDate = gdt1.getDate(); // Replace u_start_date with your field name of Start Date
var currentDate = gdt.getDate();

var diffDays = gs.dateDiff(startDate .getDisplayValue(),currentDate .getDisplayValue(), true);
var daysIn = (diffDays/(60*60*24));

gs.print(daysIn );

}

OUTPUT:

find_real_file.png

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

 

Thank you,
Abhishek Gardade

Thanks for your efforts, this is really helping me a lot, but i may i ask why you queried the cmdb_ci table instead of the cmdb_sam_sw_install table?

Hello Othomas

Because  I am just testing the code, and I randomly picked the cmdb_ci table. NO any specific reasons.

I am not able to found cmdb_sam_sw_install table because i need to first install SAM plugin.

Thanks,

Abhishek

Thank you,
Abhishek Gardade