Skip to content

Instantly share code, notes, and snippets.

@JeroenSfdc
Created January 21, 2022 18:01
Show Gist options
  • Save JeroenSfdc/149ac9f1622d13a6ab16ab6172f6231e to your computer and use it in GitHub Desktop.
Save JeroenSfdc/149ac9f1622d13a6ab16ab6172f6231e to your computer and use it in GitHub Desktop.
/**
* Created by MO20244784 on 20/01/2022.
*/
public with sharing class PerformanceTriggerHelper {
public static void setCleaningLocationMatchingStatus(List<Performance__c> newPerformanceList) {
Set<Id> workerIds = new Set<Id>();
Set<Date> workDateList = new Set<Date>();
Set<Id> personAccountIds = new Set<Id>();
List<Booking__c> updateBookingInfo = new List<Booking__c>();
Id locationId = null;
Integer locationCount = 0;
for (Performance__c performance : newPerformanceList) {
workerIds.add(performance.WorkerId__c);
workDateList.add(performance.WorkDate__c);
personAccountIds.add(performance.CustomerContractId__c);
}
List<Booking__c> bookingLst = getBookings(workDateList, workerIds);
List<Account> cleaningLocationLst = getCleaningLocations(personAccountIds);
for (Performance__c performance : newPerformanceList) {
List<Booking__c> matchedBookingLst = new List<Booking__c>();
if (performance.RecordType.DeveloperName == 'EDC') {
for (Booking__c booking : bookingLst) {
if (performance.WorkerId__c == booking.HouseholdHelp__c && performance.WorkDate__c == booking.Date__c) {
matchedBookingLst.add(booking);
}
}
}
locationCount = 0;
for (Account account : cleaningLocationLst) {
if (account.PrimaryContactNew__c == performance.CustomerContractId__c) {
locationId = account.Id;
locationCount++;
}
}
performance.MatchingStatus__c = 'Unmatched';
switch on locationCount {
when 0 {
performance.MatchingStatus__c = 'Unmatched - No Location';
}
when 1 {
switch on matchedBookingLst.size() {
when 0 {
performance.MatchingStatus__c = 'Unmatched - No booking';
}
when 1 {
performance.Booking__r = matchedBookingLst.get(1);
performance.MatchingStatusDate__c = Date.today();
matchedBookingLst.get(1).Performance__r = performance;
updateBookingInfo.add(matchedBookingLst.get(1));
performance.MatchingStatus__c = performance.HoursQuantity__c == matchedBookingLst.get(1).Hours__c ? 'Matched' : 'Partially matched - Different Hours';
}
when else {
performance.MatchingStatus__c = 'Unmatched - Multiple Bookings';
}
}
}
when else {
performance.MatchingStatus__c = 'Unmatched - Multiple Locations';
}
}
}
}
private static List<Booking__c> getBookings(Set<Date> workDateList, Set<Id> workerIds) {
return [
SELECT Id,Name,HouseholdHelp__c,Date__c,Hours__c
FROM Booking__c
WHERE Date__c IN :workDateList AND HouseholdHelp__c IN :workerIds
];
}
private static List<Account> getCleaningLocations(Set<Id> personAccountIds) {
return [
SELECT Id,Name
FROM Account
WHERE RecordType.DeveloperName = 'CleaningLocation' AND PrimaryContactNew__c IN :personAccountIds
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment