trigger BenefitDeterminationTrigger on Benefit_Determination__c (before insert, before update) { // Step 1: Create a set to hold the RecordTypeIds and Employee_Application__c lookup field values (IDs) Set<Id> recordTypeIds = new Set<Id>(); Set<Id> employeeAppIds = new Set<Id>(); // Step 2: Collect the RecordTypeIds and Employee_Application__c IDs from the Benefit_Determination__c records for (Benefit_Determination__c bdRecord : Trigger.new) { // Add RecordTypeId to the set if (bdRecord.RecordTypeId != null) { recordTypeIds.add(bdRecord.RecordTypeId); } // Add Employee_Application__c lookup field value to the set if (bdRecord.Employee_Application__c != null) { employeeAppIds.add(bdRecord.Employee_Application__c); } } // Step 3: Query related RecordType objects using the collected RecordTypeIds Map<Id, RecordType> recordTypeMap = new Map<Id, RecordType>( [SELECT Id, DeveloperName FROM RecordType WHERE Id IN :recordTypeIds] ); // Step 4: Query related Employee_Application__c records using the collected Employee_Application__c IDs Map<Id, Employee_Application__c> employeeAppMap = new Map<Id, Employee_Application__c>( [SELECT Id, Total_Child_Cost__c FROM Employee_Application__c WHERE Id IN :employeeAppIds] ); // Step 5: Loop through the Benefit_Determination__c records and calculate the values for (Benefit_Determination__c bdRecord : Trigger.new) { if (bdRecord.Status__c == 'Active') { Decimal calculatedValue = 0; // To store the result of the calculation // Get the related Employee_Application__c record from the map (if it exists) Employee_Application__c employeeApp = employeeAppMap.get(bdRecord.Employee_Application__c); // Get the RecordType from the map using the RecordTypeId RecordType recordType = recordTypeMap.get(bdRecord.RecordTypeId); // Log values for debugging System.debug('bdRecord.TFI_Level__c: ' + bdRecord.TFI_Level__c); System.debug('employeeApp.Total_Child_Cost__c: ' + employeeApp.Total_Child_Cost__c); System.debug('bdRecord.Biweekly_Hours__c ' + bdRecord.Biweekly_Hours__c); System.debug('RecordType: ' + recordType.DeveloperName); // Step 6: Check if the RecordType is 'FAA' and if the Employee_Application__c record exists if (recordType != null && recordType.DeveloperName == 'FAA' && employeeApp != null) { System.debug('Inside 1st condition: recordType.DeveloperName == FAA'); // Calculate the value based on TFI_Level__c and Total_Child_Cost__c if (bdRecord.TFI_Level__c == '$70K or Less') { calculatedValue = employeeApp.Total_Child_Cost__c * 0.70 * 4; } else if (bdRecord.TFI_Level__c == '$70,001 - $85K') { calculatedValue = employeeApp.Total_Child_Cost__c * 0.45 * 4; } else if (bdRecord.TFI_Level__c == '$85,001 - $100K') { calculatedValue = employeeApp.Total_Child_Cost__c * 0.30 * 4; } else { calculatedValue = 0; } } else { // Step 7: If the RecordType is not 'FAA', calculate based on Biweekly_Hours__c if (bdRecord.Biweekly_Hours__c > 0 && bdRecord.Biweekly_Hours__c < 80) { System.debug('Inside Else: 1st condition: bdRecord.Biweekly_Hours__c > 0 && bdRecord.Biweekly_Hours__c < 80'); // Pro-rate the calculation based on Biweekly_Hours__c if (bdRecord.TFI_Level__c == '$70,001 - $80K') { calculatedValue = bdRecord.Biweekly_Hours__c / 80 * 375.00; } else if (bdRecord.TFI_Level__c == '$80,001 - $90K') { calculatedValue = bdRecord.Biweekly_Hours__c / 80 * 333.33; } else if (bdRecord.TFI_Level__c == '$70K or less' || bdRecord.TFI_Level__c == '$90K or less') { calculatedValue = bdRecord.Biweekly_Hours__c / 80 * 416.66; } else if (bdRecord.TFI_Level__c == '$90,001 - $120K') { calculatedValue = bdRecord.Biweekly_Hours__c / 80 * 391.66; } else if (bdRecord.TFI_Level__c == '$120,001 - $150K') { calculatedValue = bdRecord.Biweekly_Hours__c / 80 * 350.00; } else { calculatedValue = 0; } } else { // Fixed values based on TFI_Level__c when Biweekly_Hours__c is not between 0 and 80 System.debug('Inside else 2nd condition: else'); if (bdRecord.TFI_Level__c == '$70,001 - $80K') { calculatedValue = 375.00; } else if (bdRecord.TFI_Level__c == '$80,001 - $90K') { calculatedValue = 333.33; } else if (bdRecord.TFI_Level__c == '$70K or less' || bdRecord.TFI_Level__c == '$90K or less') { calculatedValue = 416.66; } else if (bdRecord.TFI_Level__c == '$90,001 - $120K') { calculatedValue = 391.66; } else if (bdRecord.TFI_Level__c == '$120,001 - $150K') { calculatedValue = 350.00; } else { calculatedValue = 0; } } } // Step 8: Log the calculated value before assigning it to the field System.debug('Calculated Value for Benefit_Determination__c record: ' + calculatedValue); // Step 9: Assign the calculated value to the Max_Final_Amount_Test__c field bdRecord.Max_Final_Amount_Test__c = calculatedValue; } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter