Same license name is getting popped everytime when I trigger the notification

kiran kumar m1
Tera Contributor

Whenever the end date of a license in my alm_license table  is 10 days away to get expire , it should send a notification to a particular group. For this I created 
1.Schedule Job
2.Event
3.Notification

 

Whenever I am running this script in background script its working fine , but when I am running the scheduled job , it is triggering a notification but with a same license name everytime and that license end date is not even near to expiration.
Can anyone check why I am getting always the same license on the notification

Schedule Job

var gr = new GlideRecord('alm_license');
var today = new GlideDateTime();
gr.addNotNullQuery('end_date');
gr.query();
var found= false;
while (gr.next()){
    var enddate=new GlideDateTime(gr.end_date);
    var notifydate=new GlideDateTime(enddate);
    notifydate.addDaysUTC(-10);
    if(today.getNumericValue()>=notifydate.getNumericValue()){
        found=true;
        gs.info("license" +gr.sys_id);
        var bt=gr.display_name;
        //var kt=gr.end_date;

    }
}
if(found){
    gs.info('yes');
    //found =true;
    gs.eventQueue('license.expire',gr,enddate.toString(),bt.toString());

}
else{
    gs.info('no');
    //found = false;
}
 
Event
 
kirankumarm1_2-1741604682036.png

 

 
Notification
 
kirankumarm1_0-1741604562136.pngkirankumarm1_1-1741604591761.png

 


1 REPLY 1

PraveenK1149237
Tera Expert

The issue you're experiencing is likely due to the placement of the gs.eventQueue() function outside of the while (gr.next()) loop. When the loop finishes, the gr object will still hold the values of the last record it processed, which is why the same license is being used in the notification.

To fix this, you should move the gs.eventQueue() function inside the loop so that it triggers the event for each license that is about to expire, rather than just the last one.



var gr = new GlideRecord('alm_license');
var today = new GlideDateTime();
gr.addNotNullQuery('end_date');
gr.query();
var found = false;

while (gr.next()) {
var enddate = new GlideDateTime(gr.end_date);
var notifydate = new GlideDateTime(enddate);
notifydate.addDaysUTC(-10);

if (today.getNumericValue() >= notifydate.getNumericValue()) {
found = true;
var bt = gr.display_name;

gs.info("License " + gr.sys_id + " is about to expire.");
gs.eventQueue('license.expire', gr, enddate.toString(), bt.toString());
}
}

if (!found) {
gs.info('No licenses found expiring in 10 days.');
}
try this code and let me know if its working