New to writing triggers, I'm trying to set one up that adjusts a field on existing records with the same Owner as the one(s) being inserted.
I have an End Date field that I want populated when a new record is inserted, but I don't want to populate the inserted record's End Date.
Am I on the right track with the before insert, or should this be after? And should I be doing an update DML operation at the end?
trigger Task_End_Date on Tasks__c (before insert) {
Set<Id> task_owners = new Set<Id>();
for (Tasks__c o : Trigger.new) {
task_owners.add(o.OwnerId);
}
List<Tasks__c> other_tasks = [SELECT Id, OwnerId, End_Date__c
FROM Tasks__c
WHERE OwnerId IN :task_owners];
for (Tasks__c open_tasks : other_tasks) {
if (open_tasks.End_Date__c == null) {
open_tasks.End_Date__c = datetime.now();
}
}
}