Scheduled jobs fire email notification for specific roles and Last login time 30 days ago

Community Alums
Not applicable

Hello,

I have a requirement that scheduled jobs will fire notification to the specific roles and last login time 30 days ago. How to execute this?

3 REPLIES 3

Maddysunil
Kilo Sage

@Community Alums 

You can try with below sample script:

 

// Query users whose last login time is 30 days ago or earlier
var thirtyDaysAgo = gs.daysAgoStart(30);
var userQuery = "last_login<=javascript&colon;gs.dateGenerate('" + thirtyDaysAgo + "')";
var userGr = new GlideRecord('sys_user');
userGr.addEncodedQuery(userQuery);
userGr.query();

// Iterate through the list of users
while (userGr.next()) {
    // Get user details
    var userName = userGr.getValue('name');
    var userEmail = userGr.getValue('email');
    var lastLogin = userGr.getValue('last_login');

    // Send notification to specific roles
    var notification = new GlideRecord('sys_notification');
    notification.initialize();
    notification.insert();
    notification.setValue('subject', 'Notification for Last Login');
    notification.setValue('message', 'Dear User,\n\nYour last login was on ' + lastLogin + '. Please log in to stay updated.');
    notification.setValue('user', userGr.getValue('sys_id')); // Assuming there's a field to link notifications to users
    notification.setValue('role', 'YOUR_SPECIFIC_ROLE'); // Specify the role to send notification to
    notification.setValue('send_self', 'true'); // Send notification to the user as well
    notification.setValue('notification_type', 'email');
    notification.setValue('recipient', userEmail); // Set recipient email

    notification.update();
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Community Alums
Not applicable

Hello @Maddysunil 

It is better to try fire the event in scheduled job?  

@Community Alums 

Yup, You can do that!