Scheduled jobs fire email notification for specific roles and Last login time 30 days ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 12:59 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 01:50 AM
@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: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 01:53 AM
Hello @Maddysunil
It is better to try fire the event in scheduled job?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 01:54 AM
@Community Alums
Yup, You can do that!