Skip to content

Instantly share code, notes, and snippets.

@cmcdevitt
Created May 22, 2014 18:25
Show Gist options
  • Select an option

  • Save cmcdevitt/ea95eb945ea4f03ab914 to your computer and use it in GitHub Desktop.

Select an option

Save cmcdevitt/ea95eb945ea4f03ab914 to your computer and use it in GitHub Desktop.
List users who did not report time servicenow
//cm 5/21/2014
//Find the people who don't report time! and summarize the people who do...
//u_time_card_summary: u_total_hours,u_week_starts_on,u_user
//We are going to need time card
var tc = new GlideRecord('time_card');
//We are going to need time card summary
var tcs = new GlideRecord('u_time_card_summary');
//Get sys_id for Role of ITIL
var itil = getSysId ('ITIL', 'sys_user_role');
//gs.log("ITIL: " + itil);
//Find all users with the ITIL role
var loopcnt = 0;
var cnt = 0;
var tc_total = 0;
var ga = new GlideAggregate('sys_user_has_role');
var gaSQ = ga.addJoinQuery('sys_user','user','sys_id'); //Subquery the sys_user table
ga.addQuery('role', itil); // All Users with ITIL; For now assume that IF itil THEN they need to report time....
gaSQ.addCondition('user_name','STARTSWITH','p'); //Subquery to find accounts that start with 'p'
ga.addAggregate('COUNT');
ga.groupBy('user');
ga.query();
while (ga.next()){
//u_time_card_summary: u_total_hours,u_week_starts_on,u_user
//For each user, find last weeks time cards and total them up. Then update u_time_card_summary
var total_for_week = 0; //I'm assumming they did not enter any time.
var report_week = getLastWeek(getStartOfWeek());
tc.initialize();
tc.addQuery('user', ga.user);
tc.addQuery('week_starts_on', report_week);
tc.query();
while(tc.next()){
cnt++;
tc_total += tc.total;
total_for_week += tc.total;
}
//gs.log("User: " + ga.user + ":" + report_week + ":" + total_for_week);
tcs.initialize();
tcs.u_user = ga.user;
tcs.u_week_starts_on = report_week;
tcs.u_total_hours = total_for_week;
gs.log("wtf: " + tcs.insert());
loopcnt++;
}
gs.log("TimeCards: " + cnt + " Total: " + tc_total + " ITIL: " + loopcnt);
//**** Helper Functions ****
function getSysId (itemName,tableName){
//Todo: add varable incase the 'name' field is not correct
var gdRec = new GlideRecord(tableName);
gdRec.addQuery('name', '=', itemName);
gdRec.query();
if(gdRec.next()){
return gdRec.sys_id;
}else{
return false;
}
}
function getStartOfWeek(){
var created = new GlideDateTime(); //now
var dtUtil = new DateTimeUtils(); //Helper class
var firstDay = gs.getProperty("com.snc.time_card.start_day", 7); //default to Sunday
var cardStart = dtUtil.getWeekStart(created, firstDay).getDisplayValue(); //GlideDate
//gs.log("CARDSTART This week: " + cardStart);
return cardStart;
}
function getLastWeek(currentWeek){
var sevenDaysMilliSeconds = 604800000;
var objLastWeek = new GlideDateTime(currentWeek + ' 00:00:00');
objLastWeek.subtract(sevenDaysMilliSeconds);
return objLastWeek.getValue().split(" ")[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment