I am attempting to write a trigger and I relatively new to both Salesforce and Object oriented programming and I am struggling, so any help would be appreciated!
Task Description:
I have two custom objects, Employee, and Position Associate. Position Associate is an object that creates a mapping and record between an employee and a respective position that they hold. I am attempting to write a trigger, that runs any time a Position Associate is created or updated, that takes three fields, Training Start, Training End, and Home Study Start, and populates it with data from the employee object, from the employee that is mapped to the Position Associate record in question. This must be a trigger as opposed to a making the field a lookup value. So while that may be simpler, it is not an option in this scenario
Current Behavior: While I am not getting any errors, after a record is updated, the fields still remain blank.
Code:
trigger MapDatesPA on Position_Employee__c(after insert, after update) {
// Create trigger that runs when a new record is inserted or when a record is updated that maps the employee fields
// of Home Study Start Date, Training Start and End Dates to Position Associate Object
// Prevent Iterative Loop
if (checkRecursive.runOnce()) {
Map<Id, Employee__c> pm = new Map<Id, _Employee__c>();
Set<Id> empIds = new Set<Id>();
for (Position_Employee__c posEmp : Trigger.new) {
if (posEmp.Employee__c != null) {
empIds.add(posEmp.Employee__c);
}
}
for (Employee__c emp : [
SELECT Id, HOME_STUDY_START_DATE__c, Training_Start_Date__c, Training_End_Date__c
FROM Employee__c
WHERE Id IN :empIds
]) {
if (pm.containsKey(emp.Id)) {
pm.put(emp.id, emp);
}
}
for (Position_Employee__c posemp : Trigger.New) {
if (pm.containsKey(posemp.Id)) {
posemp.Training_Start_Date__c = pm.get(posemp.Id).Training_Start_Date__c;
posemp.Training_End_Date__c = pm.get(posemp.Id).Training_End_Date__c;
posemp.Home_Study_Start_Date__c = pm.get(posemp.Id).HOME_STUDY_START_DATE__c;
}
}
}
}