Help me in creating a function in a script include X to email managers of users.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 08:26 AM
We have a table of timesheet called TS with each record having fields of hours ,user ,week and state . Each period starts from Saturday and ends at Sunday. Our working days are Mon-Friday. There is a script include X where I have to write a function which queries the timesheet table (TS),finds out which user in the last week had his timesheet entry's hours <37.5 and state as "Not Processed"(it's a drop down field with options) then shoot an email to all managers of users who fall in the above criteria on the following Monday at 5pm. The mail sent to each manager should have list of users (who report to them) from the previous week who fall in the above criteria.
Note- We have an event made which is called 'managerCall' which is linked to a notification which shall be sent to the manager with the list of his/her reportees (this part we need to write in the function to obtain) . The function which I will be making will fire the event and this function will be called in scheduled job.
Ask me any questions for better clarification. Your help will be appreciated !
- Labels:
-
Service Catalog
-
Service Desk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2024 04:32 AM
Hi @Aditya Sinha ,
PFB,
var X = Class.create();
X.prototype = {
initialize: function() {},
findUsersWithIncompleteTimesheets: function() {
var lastWeekStart = new GlideDateTime();
lastWeekStart.addWeeks(-1);
lastWeekStart.setDayOfWeek(7); // Set to Saturday of last week
lastWeekStart.setHourOfDay(0);
lastWeekStart.setMinute(0);
lastWeekStart.setSecond(0);
var lastWeekEnd = new GlideDateTime();
lastWeekEnd.addWeeks(-1);
lastWeekEnd.setDayOfWeek(1); // Set to Sunday of last week
lastWeekEnd.setHourOfDay(23);
lastWeekEnd.setMinute(59);
lastWeekEnd.setSecond(59);
var gr = new GlideRecord('TS');
gr.addQuery('week', lastWeekStart.getDate().getWeekOfYear());
gr.addQuery('state', 'Not Processed');
gr.addQuery('hours', '<', 37.5);
gr.addQuery('week_start', '>=', lastWeekStart);
gr.addQuery('week_end', '<=', lastWeekEnd);
gr.query();
var managerMap = {};
while (gr.next()) {
var user = gr.user.toString();
var manager = this.getManager(user);
if (!managerMap[manager])
managerMap[manager] = [];
managerMap[manager].push(user);
}
for (var manager in managerMap) {
if (managerMap.hasOwnProperty(manager)) {
var reportees = managerMap[manager].join(', ');
// Fire the 'managerCall' event here and pass the reportees to it
this.fireManagerCallEvent(manager, reportees);
}
}
},
getManager: function(user) {
// Custom function to get the manager of a user
// You need to implement this function according to your system's logic
},
fireManagerCallEvent: function(manager, reportees) {
// Custom function to fire the 'managerCall' event
// You need to implement this function according to your system's logic
},
type: 'X'
};
In this script include X, we have a function findUsersWithIncompleteTimesheets that queries the TS table for timesheet entries meeting the specified criteria for the last week. It then maps each user to their manager and sends an email notification to each manager with the list of their reportees who fall under the criteria.
You need to implement the getManager and fireManagerCallEvent functions according to your system's logic. The getManager function should return the manager of a given user, and the fireManagerCallEvent function should trigger the 'managerCall' event with the appropriate parameters (manager and reportees).
Once you've implemented these functions, you can call the findUsersWithIncompleteTimesheets function from a scheduled job to execute this logic.
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Thanks,
Amitoj Wadhera