Need to trigger notification based on schedule job but event log status showing error

mani55
Tera Contributor

Based on schedule job i need to trigger notification but present event is triggered but event status showing error and notification is not triggered so please let me know the mistake.

 

schedule script:

 

var currentMonth = new GlideDateTime().getMonth(); // Get the current month (0-11)

// Check if the current month is July (6) or November (10)
if (currentMonth == 6 || currentMonth == 12) {
    var risk = new GlideAggregate('u_transversal_code');
risk.groupBy('u_user');
risk.query();
while (risk.next()) {
	var created_by = new GlideRecord("sys_user");
	created_by.addQuery('sys_id',risk.u_user.sys_id);
	created_by.query();
	if(created_by.next()){	
        gs.eventQueue('access_rights_renewal',risk,null,risk.u_user);
	}
	
}
    }

 

event :

 

mani55_0-1730783029286.png

Notification

 

mani55_1-1730783061458.png

 

1 ACCEPTED SOLUTION

Hello @mani55 

As @GlideFather  rightly pointed out, the event expects a GlideRecord object, which necessitated some modifications to the code. Here’s the updated script:

 

var risk = new GlideAggregate('u_transversal_code');
risk.addAggregate('COUNT');
risk.groupBy('u_user'); // Group by u_user to get unique users
risk.query();

while (risk.next()) {
    var created_by = new GlideRecord('sys_user');
    created_by.addQuery('sys_id', risk.getValue('u_user'));
    created_by.query();
    if (created_by.next()) {
        gs.eventQueue('access_rights_renewal', created_by, null, created_by.getValue('sys_id'));
    }
}

 

This revised code will trigger the event for each unique user and send notifications accordingly. This works in my PDI.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

View solution in original post

9 REPLIES 9

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @mani55 
On which table the event "access_rights_renewal" is created, if it is on "u_transversal_code", then you have too pass the GlideRecord object of this table but in your script you are passing a GlideAggregate object.


Thanks and Regards,

Saurabh Gupta

 @Saurabh Gupta   i used below scripting in background script for testing now it's working fine but one bug is there u_transversal_code in this table we have total 26000 records but when group by user field we have only 350 users so we need to send notification only 350 users but now it's triggered 26000 records

 

  var risk = new GlideRecord('u_transversal_code');
risk.groupBy('u_user');
risk.query();
while (risk.next()) {
	var created_by = new GlideRecord("sys_user");
	created_by.addQuery('sys_id',risk.u_user.sys_id);
	created_by.query();
	if(created_by.next()){	
        gs.eventQueue('access_rights_renewal',risk,null,risk.u_user);
	}
}

Juhi Poddar
Kilo Patron

Hello @mani55 

Please try the below code: 

var risk = new GlideAggregate('u_transversal_code');
risk.addAggregate('COUNT');
risk.groupBy('u_user'); // Group by u_user to get unique users
risk.query();

while (risk.next()) {
    var userId = risk.getValue('u_user'); // Get the unique user's sys_id
    if (userId) {
        gs.eventQueue('access_rights_renewal', risk, null, userId); // Send the event only once per user
    }
}

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

 

it's not working event logs state is showing error

 

mani55_0-1730792487290.png