Schedule Job script

AnanyaT
Tera Contributor

I need a script that triggers the event when the asset from hardware table is expiring in 15 days.

 

5 REPLIES 5

Viraj Hudlikar
Giga Sage

Hello @AnanyaT 

 

Below is sample script to check asset is expiring in next 15 days and trigger event.

(function executeSchedule(current, scope) {
    // Calculate target expiration date
    var targetDate = new GlideDateTime();
    targetDate.addDaysLocalTime(15);
    
    // Query alm_hardware assets expiring in 15 days
    var hardwareGR = new GlideRecord('alm_hardware');
    hardwareGR.addQuery('install_status', '1'); // Example: In Use status
    hardwareGR.addQuery('warranty_expiration', targetDate.getDate());
    hardwareGR.query();

    while (hardwareGR.next()) {
        gs.eventQueue('asset.expiration.notice', hardwareGR, hardwareGR.sys_id, gs.getUserID());
    }

})(current, scope);

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @AnanyaT 

I hope your concern has been fully addressed. If it resolves your issue, please consider marking it as the accepted solution. This will ensure others in the community can benefit from the solution too.


As per new community feature you can mark multiple responses as correct.


Thanks & Regards
Viraj Hudlikar.

mayankkumar
Tera Sage

Hi @AnanyaT,
---------------------------------------------------------------------------------------------------------------------------------------------------
You have to configure a Scheduled Script Execution (sysauto_script.do) and an Event to fire from the script of Schedule Job.
which Runs daily Time as per your requirement
Here is the script that triggers the event when the asset from hardware table is expiring in 15 days.

 var gr = new GlideRecord('alm_hardware');
    var targetDate = new GlideDate();
    targetDate.addDaysUTC(15); // 15 days from now

    gr.addNotNullQuery('warranty_expiration'); // or your expiry field
    gr.addQuery('warranty_expiration', targetDate);
    gr.query();

    while (gr.next()) {
        gs.eventQueue('asset.expiring_soon', gr, gr.sys_id, gs.getUserID());
        gs.info('Event triggered for expiring asset: ' + gr.name);
    }

Replace the Event Name and the Params according to you
Let me if you need further help.
---------------------------------------------------------------------------------------------------------------------------------------------------
Please mark my response helpful and accept as solution
Thanks & Regards
Mayank

kaushal_snow
Mega Sage

Try with this logic

var gr = new GlideRecord('alm_hardware');
gr.addQuery('warranty_expiration', '<=', gs.daysAgo(-15));
gr.addQuery('warranty_expiration', '>=', gs.daysAgo(-16)); 
gr.query();
while (gr.next()) {
gs.eventQueue('hardware.expiring.in_15_days', gr, gr.sys_id, gr.warranty_expiration.getDisplayValue());
}

 

Trigger the event 'hardware.expiring.in_15_days' and pass parameters such as sys_id and expiration date.

 

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/