PLs advise the correction in the script

BanuMahalakshmi
Tera Contributor

Hi,

My requirement is - The scheduled job should run the check computer login date(check_in), if the checkin date exceeded 30 days from the current date, the system will trigger event for email notification.

if the checkin date exceeded 35 days, the system will create incident. if the check in  date exceeded 50days, the system Check for active incident that matches this criteria:

  • Incident CI = sys ID of Ci being checked in scheduled script
  • Incident short description starts with "PC has not login"
  • If active incident exists:
    • Update incident work note to add:   PC has been login for 50 days
var currentTime;
var days;
var checkin;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_updated_by=admin^sys_class_name=cmdb_ci_computer^attested=false');
gr.query();
if (gr.next()) {
    currentTime = gs.nowDateTime();
    checkin = gr.checked_in.getDisplayValue();
    diff = gs.dateDiff(checkin, currentTime, true) / 86400;
    days = Math.floor(diff);
    if (days == '30') {
        gs.eventQueue("PC Email Notification", gr.short_description, gr.checked_in);
    } else if (days == '35') {
        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.state = 1;
        incGr.urgency = 2;
        incGr.contact_type = 'self-service';
        incGr.caller_id = " System Administrator";
        incGr.short_description = " PC has not login";
        incGr.insert();


    }
 }
6 REPLIES 6

Mark Manders
Mega Patron

Just use a flow: https://www.servicenow.com/community/developer-forum/create-scheduled-job/td-p/3075315


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

My Client requirement is create scheduled job and they didnt agree for flow design.

Moin Kazi
Kilo Sage
Kilo Sage

Hi @BanuMahalakshmi ,

 

You can add 1 more else if condition and update your incident if days exceed 55 days. and please also put while loop instead of if in your first gr object-

[Notes: Hope you are running daily this scheduled job]

 

else if(days == '55'){
var inc = new GlideRecord("incident");
inc.addQuery('cmdb_ci', gr.sys_id);
inc.addQuery('short_description', 'PC has not login');
inc.addActiveQuery();
inc.query();
if (inc.next()) {
inc.work_notes = "PC has been login for 50 days";
inc.update();
}

 

var currentTime;
var days;
var checkin;
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_updated_by=admin^sys_class_name=cmdb_ci_computer^attested=false');
gr.query();
while (gr.next()) {
    currentTime = gs.nowDateTime();
    checkin = gr.checked_in.getDisplayValue();
    diff = gs.dateDiff(checkin, currentTime, true) / 86400;
    days = Math.floor(diff);
    if (days == '30') {
        gs.eventQueue("PC Email Notification", gr.short_description, gr.checked_in);
    } else if (days == '35') {
        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.state = 1;
        incGr.urgency = 2;
        incGr.contact_type = 'self-service';
        incGr.caller_id = " System Administrator";
        incGr.short_description = " PC has not login";
        incGr.insert();
    }
   else if(days == '55'){
        var inc = new GlideRecord("incident");
        inc.addQuery('cmdb_ci', gr.sys_id);
        inc.addQuery('short_description', 'PC has not login');
        inc.addActiveQuery();
        inc.query();
        if (inc.next()) {
            inc.work_notes = "PC has been login for 50 days";
            inc.update();
        }
}
 }

 

Hope this help you.

 

Regards,

Moin

Brad Bowman
Kilo Patron
Kilo Patron

There were a few issues with the original script:

1) only returning one record from the cmdb_ci_computer GlideRecord - change 'if' to 'while'

2) not setting the incident configuration item when the incident is created

 

Here's one way to correct all of this plus incorporate updating the incident after 50 days:

 

var currentTime = '';
var days = 0;
var checkin = '';
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sys_updated_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_updated_by=admin^sys_class_name=cmdb_ci_computer^attested=false');
gr.query();
while (gr.next()) {
    currentTime = gs.nowDateTime();
    checkin = gr.checked_in.getDisplayValue();
    diff = gs.dateDiff(checkin, currentTime, true) / 86400;
    days = Math.floor(diff);
	if (days == 30) {
		gs.eventQueue("PC Email Notification", gr.short_description, gr.checked_in);
    } else if (days == 35) {
        var incGr = new GlideRecord("incident");
        incGr.initialize();
        incGr.state = 1;
        incGr.urgency = 2;
        incGr.contact_type = 'self-service';
        incGr.caller_id = "System Administrator"; //replace with sys_id of user account
        incGr.short_description = "PC has not logged in";
		incGr.cmdb_ci = gr.sys_id;
        incGr.insert();
    } else if (days == 50) {
		var inc = new GlideRecord("incident");
		inc.addQuery('cmdb_ci', gr.sys_id);
		inc.addQuery('short_description', 'PC has not logged in');
		inc.query();
		if (inc.next()) {
			inc.work_notes = "PC has not logged in for 50 days.";
			inc.update();
		}
	}
}