function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mthobisi Ndlovu 2Mthobisi Ndlovu 2 

How to Add up values, Same field different records

I am trying to create a Timesheet like application, there's a requirement that it should check the total number of hours (Hours Worked object) captured by the user in the previous week. If the hour worked are less than 40 hours an email reminder must be sent to the user. I created a formula field that checks the weeks the record was created. There's also a field called Billing Type which has options "Billable" and "Non Billable".. which means a user can have more hours worked records for one specific day depending on how many hours were billable and how many were not.

Scenario
user1: captures hours worked (records) for Monday - 4.5 hours ( Billable)& 3.5 (Non Billable), Tuesday - 4 Hours (Billable), Wednesday - 8 hours(Non billable), Thursday - 6 hours (Billable) & 1.5 Hours (Non Billable) , Friday - 0 hours captured (user forgot to capture them).

user2: captures hours worked (records) for Monday - 8 hours ( Billable), Tuesday - 8 Hours (Billable), Wednesday - 0, Thursday - 8hours  (Non Billable) , Friday - 8hours (Non Billable).

Since the total hours worked the previous week are less than 40 hours, On Monday morning the user will recieve an reminder to complete the hours worked. Schedulable class will be used for this.

I have create the following which will be called by the Schedulable class.
public with sharing class HoursWorkedService {


	public void sendTimesheetEmailReminder() {
		Decimal totalHours = 0;
		Set<Id> hoursWorkedIdSet = New Set<Id>();
		List<Hours_Worked__c> hoursWorkedIList = [SELECT Id From Hours_Worked__c];
		
		for (Hours_Worked__c hoursWorked: hoursWorkedIList){
			hoursWorkedIdSet.Add(hoursWorked.Id);
		} 
		
		List<Hours_Worked__c> hoursWorkedPreviousWeekList = New HoursWorkedSelector().selectPreviousWeekById(hoursWorkedIdSet);
		
		if (hoursWorkedPreviousWeekList.size() > 0) {
			for(Hours_Worked__c previousWeek: hoursWorkedPreviousWeekList){
				 totalHours = totalHours + previousWeek.Hours__c;
				if(totalHours < 40){
					System.debug('Please complete your timesheet '+totalHours);
				}
			}
		} 
	}
	
	
}

As you can see all I'm doing is just incrementing not sure if it will work, what I would like to know is,

1. How do I add up previous week hours for a specific user (user1) only.
2. The proper way of doing the calculation.
3. Sending the email alert to the user.

Thank you.
Mthobisi Ndlovu 2Mthobisi Ndlovu 2
Update:

I believe I have found the answer to question 1 & 2 .. update code below. I am still looking for 3 (sending the email alert). I used the a Map  and createdById  as the key for that Map to Identify a specific user.

if there's a better way of doing this please feel free to comment I'm pretty much a novice when it comes to Apex.
 
public with sharing class HoursWorkedService {


	public void sendTimesheetEmailReminder() {
		
		Set<Id> hoursWorkedIdSet = New Set<Id>();
		Decimal totalHoursWorked = 0;
		List<Hours_Worked__c> hoursWorkedIList = [SELECT Id From Hours_Worked__c];
		
		for (Hours_Worked__c hoursWorked: hoursWorkedIList){
			hoursWorkedIdSet.Add(hoursWorked.Id);
		} 
		
		List<Hours_Worked__c> hoursWorkedPreviousWeekList = New HoursWorkedSelector().selectPreviousWeekById(hoursWorkedIdSet);
		if (hoursWorkedPreviousWeekList.size() > 0) {
			totalHoursWorked = calculateTotalHours(hoursWorkedPreviousWeekList);
			if(totalHoursWorked < 40){
				System.debug('Email Alert: Please complete your timesheet '+totalHoursWorked);
			}
		} 
	}
	
	private static Decimal calculateTotalHours(List<Hours_Worked__c> hoursWorkedList){
		Map<Id, List<Double>> totalHourWorkedMap = New Map<Id, List<Double>>();
		List<Double> hoursList = New List<Double>();
		Decimal totalHours = 0;
		
		
		if(!hoursWorkedList.isEmpty()) {
			for(Hours_Worked__c hourWorked: hoursWorkedList ){
				if(totalHourWorkedMap.get(hourWorked.createdById)== null){
					totalHourWorkedMap.put(hourWorked.createdById, New List<Double>{hourWorked.Hours__c});
					hoursList = totalHourWorkedMap.get(hourWorked.CreatedById);
				}else {
					totalHourWorkedMap.get(hourWorked.createdById).add(hourWorked.Hours__c);
					hoursList = totalHourWorkedMap.get(hourWorked.CreatedById);
				}
			}
		}
	 	
	 	if (hoursList.size() > 0){
			for(Decimal hrs: hoursList){
				totalHours += hrs;
			}
	 	}
	 	
		return totalHours;
	}
}