+ Start a Discussion
José Teixeira GomesJosé Teixeira Gomes 

Follow up Tasks for Shared Events

So I have been trying to create a trigger to create follow up Tasks automatically, when a given Task is completed with somes dependencies to Contacts and Account fields, and with substantial help from this forum I have been able to do so. Here is the code:
Trigger FollowUpTask on Task(after update) {

     Set<Id> completedTasks = new Set<Id>();
     Set<Id> contactIds = new Set<Id>();
     Set<Id> accountIds = new Set<Id>();
     for (Task task: Trigger.new) {
          if (task.Status != Trigger.oldMap.get (task.Id).Status && task.Status=='Completed')
     Map<Id, Contact> contactMap = new Map<Id, Contact>();
     Map<Id, Account> accountMap = new Map<Id, Account>();
     if (!contactIds.isEmpty()) {
          contactMap = new Map<Id, Contact>([
               select Contact_Frequency__c,
               from Contact
               where Id in :contactIds
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
               from Account
               where Id in :accountIds

     List<Task> newTasks = new List<Task>();
     for (Id id: completedTasks) {
          Task task = Trigger.newMap.get(id);
          Contact contact = contactMap.get(task.WhoId);
          Account account = accountMap.get(task.WhatId);
          if (account == null || contact == null) {
          Task newTask = new Task(
               Subject = 'Follow Up ' + account.Name + '/' + contact.Name,
               OwnerId = task.OwnerId,
               WhoId = account.Primary_Contact__c,
               WhatId = account.Id,
               Priority = (account.Tier__c == 'Tier 1') ? 'High' : 'Normal'
          Date duedate = system.today().addDays((Integer)(contact.Contact_Frequency__c));
          newTask.ActivityDate = dueDate;
     if (!newTasks.isEmpty()) {
          insert newTasks;
But now I was wondering if something similar is possible for Events, with multiple related contacts. The objective would be: when an event (with several contacts) was completed, the trigger would create individual followup tasks for each individual contact in a similar way the code above did. Is it possible to adapt the code above (an event trigger) to satisfy this requirement? I guess the modification would be to check each WhoID's parent account for the WhatID of the new task, as opposed to using the task's WhatID.

All help is appreciated

Best Answer chosen by José Teixeira Gomes
Yes, your approach seems to be correct, but just that you are aware, Salesforce has OOTB ability to create Follow up Tasks and Events by clicking a button on the Task/Event Detail page. I know your use case is little different, so trigger approach is an optimal way.