global class CreateDailyDialListTasksForAgents implements Schedulable {
global void execute(SchedulableContext SC) {
String reportName = 'DQ_Account_Buckets';
List<Report> reportList = [SELECT Id, DeveloperName FROM Report WHERE DeveloperName = :reportName];
if (!reportList.isEmpty()) {
fetchReportData(reportList[0].Id);
}
}
global void fetchReportData(Id reportId){
Reports.ReportResults results = Reports.ReportManager.runReport(reportId, true);
if(results != null && results.getFactMap() != null){
List<String> headers = new List<String>();
for(String colName : results.getReportMetadata().getDetailColumns()) {
String label = results.getReportExtendedMetadata().getDetailColumnInfo().get(colName).getLabel();
headers.add(label);
}
Reports.ReportFactWithDetails factDetails = (Reports.ReportFactWithDetails)results.getFactMap().get('0!0_0');
Map<String,String> accPhoneMap = new Map<String,String>();
for (Reports.ReportDetailRow row : factDetails.getRows()) {
Integer cellIndex = 0;
String Name;
String Phone;
for (Reports.ReportDataCell cell : row.getDataCells()){
String column = headers.get(cellIndex);
if(Column == 'Property Owner: Account Name'){
Name = cell.getLabel();
}
if(Column == 'Property Owner: Phone'){
Phone = cell.getLabel();
}
cellIndex++;
}
accPhoneMap.put(Name,Phone);
}
system.debug(accPhoneMap);
if(accPhoneMap != null && !accPhoneMap.isEmpty()){
createTasksForAgents(accPhoneMap);
}
}
}
global void createTasksForAgents(Map<String,String> accPhoneMap){
List<Id> usersIds = new List<Id>();
List<Id> availableUsersIds = new List<Id>();
/* for(GroupMember gm :[Select UserOrGroupId From GroupMember where Group.Name ='Collection Queue']){
String uId = gm.UserOrGroupId;
if(uId.startsWith('005')){
usersIds.add(uId);
}
}*/
for(User u :[Select Id From User where Profile.Name ='HOA Account Loyalty' OR Profile.Name ='HOA Account Loyalty Mgr']){
usersIds.add(u.Id);
}
if(!usersIds.isEmpty()){
for(UserServicePresence usp :[Select UserId From UserServicePresence Where IsCurrentState = true and UserId IN:usersIds]){
availableUsersIds.add(usp.UserId);
}
if(availableUsersIds.isEmpty()){
return;
}
List<Task> tasksList = new List<Task>();
for(String acc : accPhoneMap.keySet()){
Task t = new Task();
t.Subject = 'Call '+acc+' @ '+accPhoneMap.get(acc);
t.IsReminderSet = true;
t.ReminderDateTime = System.now();
t.ActivityDate = Date.today();
tasksList.add(t);
}
Integer noOfUsers = availableUsersIds.size();
Integer noOfTasks = tasksList.size();
Integer recordsPerUser = (noOfTasks + noOfUsers - 1) / noOfUsers;
Map<Id, List<Task>> userTaskMap = new Map<Id, List<Task>>();
for(Id uId : availableUsersIds){
userTaskMap.put(uId, new List<Task>());
}
Integer currentIndex = 0;
for(Task t : tasksList){
String uId = availableUsersIds[Math.mod(currentIndex, noOfUsers)];
userTaskMap.get(uId).add(t);
currentIndex++;
}
List<Task> tasksToInsert = new List<Task>();
for(Id uId : userTaskMap.keySet()){
List<Task> tasks = userTaskMap.get(uId);
for (Task t : tasks) {
t.OwnerId = uId;
tasksToInsert.add(t);
}
}
Database.insert(tasksToInsert,false);
}
}
}