1

I want a trigger on after update to update Employee records (custom), on after update there are some other related Employees should be updated with the values of the fields in the newly updated accounts;

see the table below;

enter image description here

If the location of the employee has changed in the updated record, then the updated location value in the existing record should be populated through the other employee records, which has the similar status ID.

So the result should be ; enter image description here

  1. How could I achieve this?
  2. Can I call update after update?

Thanks in advance.

1 Answer 1

1

You will need a single AFTER UPDATE Trigger. All you will have to do is update the other employees for the same statusId without firing a DML on the employee for which location has changed.

So, You can wrap something like below in your method (This is just an impression)-

List<Employee__c> newEmployeeList = trigger.new;
Map<Id,Employee> oldEmployeeMap = trigger.oldMap;
Map<String,String> empByLocationMap = Map<Id,String>();
List<Employee__c> toBeUpdatedEmployeeList = new List<Employee__c>();
for(Employee__c emp : newEmployeeList){
    if(emp.Location__c != oldTaskMap.get(emp.id).Location__c){
        empByLocationMap.put(emp.StatusId__c,emp.Location__c);
        empIds.add(emp.Id);
    }
}
if(!employeeWithLocationChangedIds.IsEmpty()){
    for(Employee__c empl : [Select Id, StatusId__c, Location__c from  Employee__c where Id !=: empIds AND StatusId__c =: empByLocationMap.keyset()){
        empl.Location__c = empByLocationMap.get(empl.StatusId__c);
        toBeUpdatedEmployeeList.add(empl);
    }
}

update toBeUpdatedEmployeeList;
1
  • welcome!! glad it helped out! Aug 18, 2015 at 5:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .