Apex Triggers || Lookup Field || Record type
Tue Oct 08 2024 20:02:04 GMT+0000 (Coordinated Universal Time)
Saved by
@mdfaizi
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;
}
}
}
content_copyCOPY
salesforce not call the trigger each time for multiple record creation , it calls the trigger at once with all the creation details.
Its called bulkification
Let’s say you're updating multiple records using the Data Loader. Here's what happens:
Salesforce triggers are designed to automatically handle all the records in a single transaction.
So, instead of executing the trigger for each record one by one (which would be inefficient), Salesforce will bundle up to 200 records into a single batch and pass them to the trigger.
The trigger processes each record within the loop.
https://chatgpt.com/share/67058f8f-b858-800d-960d-344eda20e8c0
Comments