0

I have a custom object (Shift) with a date field (Start Time). I have a collection of tasks that I would like to update based on the Shift.Start Time.

One approach I've looked into is setting up a workflow rule, but I don't see an option to update an independent object like a task.

  • you should consider using a Flow (which in winter 20 can now be scheduled); the Flow can update related objects, including Tasks – cropredy Nov 2 '19 at 17:54
0

You need to use a Trigger for these type of processes if other functionalities are not enough. Please, take a look to this: Apex Triggers.

Try to use the before update rather than after update when posible. On the before update you don't need to handle DML operations.

You can do something similar like this and for this test I use the ActivityDate field on the Task object. I haven't try this but it can give you an idea of what I mean.

trigger shiftTaskUupdate on Shift__c (after update) {
 Set<Id> shiftId = new Set<Id>();

  for (Shift__c shift : System.Trigger.new){
    if ((shift.Start_Time__c !=null) &&
      (System.Trigger.isInsert ||
        (shift.Start_Time__c != System.Trigger.oldMap.get(shift.Id).Start_Time__c))) {
         shiftId.add(shift.Id);

   }

   List<Task> taskToUpdate = new List<Task>();
   List<Shift__c> shiftWithTasks = [
      SELECT Id, Start_Time__c,(SELECT Id, ActivityDate FROM Task) 
      FROM Shift__c 
      WHERE Id = :shiftId];

     for(Shift__c sh : shiftWithTasks) {
        for(Task tsk : sh.Task) {
           tsk.ActivityDate = sh.Start_Time__c;
           taskToUpdate.add(tsk);
        }
     }
     if(!taskToUpdate.isEmpty()) {
        update taskToUpdate;
     } 
}
  • Trigger will not work because the OP wants to fire the action at the date/time specified in the custom field. – David Cheng Nov 3 '19 at 1:49
  • You can use a trigger here if the Shift__c is updated and the field Start_Time__c is modified. Then you can us a soql to get any task related to the Shift__c record been updated and update the Task field with the Shift.Start Time field value. So, it is posible to use a trigger here. – Carlos Naranjo Nov 3 '19 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.