• José Teixeira Gomes
  • NEWBIE
  • 30 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 15
    Replies
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')
               completedTasks.add(task.Id);
               contactIds.add(task.WhoId);
               accountIds.add(task.WhatId);
          }
 
     contactIds.remove(null);
     accountIds.remove(null);
 
     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,
                    Name
               from Contact
               where Id in :contactIds
          ]);
     }
 
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
                    Name,
                    Tier__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) {
               continue;
          }
 
          Task newTask = new Task(
               Subject = 'Follow Up ' + account.Name + '/' + contact.Name,
               OwnerId = account.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;
 
          newTasks.add(newTask);
     }
 
     if (!newTasks.isEmpty()) {
          insert newTasks;
     }
}

Hello, I am very new to Salesforce and after having successfully written a trigger, with a great deal of help from the forum, I am having some issues with the test class. I thought it would be enough to test an example of where the trigger would be fired and see if the trigger did the job but apparently it is not, as I only got 73% of the lines tested. So I wanted your feedback on what I did wrong, any help is appreciated.

The trigger, the issues seem to be with the maps and towards the end:
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')
               completedTasks.add(task.Id);
               contactIds.add(task.WhoId);
               accountIds.add(task.WhatId);
          }
 
     contactIds.remove(null);
     accountIds.remove(null);
 
     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,
                    Name
               from Contact
               where Id in :contactIds
          ]);
     }
 
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
                    Name,
                    Tier__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) {
               continue;
          }
 
          Task newTask = new Task(
               Subject = 'Follow Up ' + account.Name + '/' + contact.Name,
               OwnerId = account.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;
 
          newTasks.add(newTask);
     }
 
     if (!newTasks.isEmpty()) {
          insert newTasks;
     }
}

And this is the test class, I am sure there are a ton of errors:
@istest
public class FollowUpTaskTest {

  static testmethod void testFollowUpTask ()
  {
      Contact Ctc = new Contact (LastName = 'TestContact',
                                 Contact_Frequency__C = 30);
      
      Account Acc = new Account (Name = 'TestAccount',
                                 Rank__C = 3,
                                 Primary_Contact__C = Ctc.Id,
                                 OwnerId = '00520000003j1B2');
       
      Date Today = System.today();
          
      Task Tsk = new Task (	Subject = 'TestTask', 
                   			OwnerId = '00520000003j1B2',
                   			WhoId = Ctc.Id,
                   			WhatId = Acc.Id,
                   			Priority = 'Normal',
                           	Status = 'Not Started',
                           	ActivityDate = Today.addDays(5)
                           	);
       insert Acc;
       insert Ctc;
       insert Tsk;
          
  		Test.startTest();
        Tsk.Status = 'Completed';
        Update Tsk;
      	Test.stopTest();
        
      Tsk = [select Id, Subject, OwnerId, WhatId, WhoId, Priority, Status, ActivityDate
                 	from Task
                 	where WhatId = :Acc.Id
                 	and WhoId = :Ctc.Id
          			and ActivityDate = :Today.addDays((integer)(Ctc.Contact_Frequency__C)) ];
      
      System.assert(Tsk.ActivityDate == Today.addDays((integer)(Ctc.Contact_Frequency__C)));
      System.assert(Tsk.OwnerId == Acc.OwnerId);
      System.assert(Tsk.Subject == 'Follow Up ' + Acc.Name + '/' + Ctc.Name);
      System.assert(Tsk.WhoId == Acc.Primary_Contact__c);
      System.assert(Tsk.WhatId == Acc.Id);
      System.assert(Tsk.Priority =='High');          
          }
    }


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')
               completedTasks.add(task.Id);
               contactIds.add(task.WhoId);
               accountIds.add(task.WhatId);
          }
 
     contactIds.remove(null);
     accountIds.remove(null);
 
     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,
                    Name
               from Contact
               where Id in :contactIds
          ]);
     }
 
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
                    Name,
                    Tier__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) {
               continue;
          }
 
          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;
 
          newTasks.add(newTask);
     }
 
     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

Hello, I am fairly new to Salesforce and even more to APEX so I am having some difficulties in writing a Trigger.

So I have managed to create a list of tasks that have been completed. Now I want to create new tasks in x number of days but I don't know how to insert a list of new tasks. All tasks would have the same structure ('Follow Up' + Account/Contact Name; assigned to the same person has before OwnerId; Not Started, WhatId the same as before). There is just a few things I need the code to do.

ActivityDate should be: Today + Contact_Frequency__c (Contact Custom Field, number field)
WhoId should be: Primary_Contact__c (Account Custom Field, lookup to contact)
Priority should be dependent on a Tier__c (Account Custom Field, formula field): High if it is Tier 1, else normal

I would have done this myself but I still don't know how to reference other objects fields

Then I have another question regarding shared activities (how to create separate tasks for the participants of a given event), but that is for another day.

All help is appreciated
Hello, I am new to Salesforce, currently still with a trial version, and I am trying to create an Apex Trigger but before I get into it I wanted to ask if my idea is even possible. So I want a trigger that fires when a task is completed and creates a task the same account in (+) a specific time period. For the trigger part I want it to: (1) change the due date to the date it is completed; (2) schedule the next task depending on an account custom field (frequency); (3) assign it to a contact of that account, but only the one that is assigned as primary (custom field). Also the trigger needs to be built so it can handle mass updates of accounts/contacts/tasks.

I have tried to build something but still have very limited knowledge. If anyone has built anything similar and would like to share it with I would be extremely appreciated. Any help is welcome! Thanks
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')
               completedTasks.add(task.Id);
               contactIds.add(task.WhoId);
               accountIds.add(task.WhatId);
          }
 
     contactIds.remove(null);
     accountIds.remove(null);
 
     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,
                    Name
               from Contact
               where Id in :contactIds
          ]);
     }
 
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
                    Name,
                    Tier__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) {
               continue;
          }
 
          Task newTask = new Task(
               Subject = 'Follow Up ' + account.Name + '/' + contact.Name,
               OwnerId = account.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;
 
          newTasks.add(newTask);
     }
 
     if (!newTasks.isEmpty()) {
          insert newTasks;
     }
}

Hello, I am very new to Salesforce and after having successfully written a trigger, with a great deal of help from the forum, I am having some issues with the test class. I thought it would be enough to test an example of where the trigger would be fired and see if the trigger did the job but apparently it is not, as I only got 73% of the lines tested. So I wanted your feedback on what I did wrong, any help is appreciated.

The trigger, the issues seem to be with the maps and towards the end:
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')
               completedTasks.add(task.Id);
               contactIds.add(task.WhoId);
               accountIds.add(task.WhatId);
          }
 
     contactIds.remove(null);
     accountIds.remove(null);
 
     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,
                    Name
               from Contact
               where Id in :contactIds
          ]);
     }
 
     if (!accountIds.isEmpty()) {
          accountMap = new Map<Id, Account>([
               select Primary_Contact__c,
                    Name,
                    Tier__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) {
               continue;
          }
 
          Task newTask = new Task(
               Subject = 'Follow Up ' + account.Name + '/' + contact.Name,
               OwnerId = account.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;
 
          newTasks.add(newTask);
     }
 
     if (!newTasks.isEmpty()) {
          insert newTasks;
     }
}

And this is the test class, I am sure there are a ton of errors:
@istest
public class FollowUpTaskTest {

  static testmethod void testFollowUpTask ()
  {
      Contact Ctc = new Contact (LastName = 'TestContact',
                                 Contact_Frequency__C = 30);
      
      Account Acc = new Account (Name = 'TestAccount',
                                 Rank__C = 3,
                                 Primary_Contact__C = Ctc.Id,
                                 OwnerId = '00520000003j1B2');
       
      Date Today = System.today();
          
      Task Tsk = new Task (	Subject = 'TestTask', 
                   			OwnerId = '00520000003j1B2',
                   			WhoId = Ctc.Id,
                   			WhatId = Acc.Id,
                   			Priority = 'Normal',
                           	Status = 'Not Started',
                           	ActivityDate = Today.addDays(5)
                           	);
       insert Acc;
       insert Ctc;
       insert Tsk;
          
  		Test.startTest();
        Tsk.Status = 'Completed';
        Update Tsk;
      	Test.stopTest();
        
      Tsk = [select Id, Subject, OwnerId, WhatId, WhoId, Priority, Status, ActivityDate
                 	from Task
                 	where WhatId = :Acc.Id
                 	and WhoId = :Ctc.Id
          			and ActivityDate = :Today.addDays((integer)(Ctc.Contact_Frequency__C)) ];
      
      System.assert(Tsk.ActivityDate == Today.addDays((integer)(Ctc.Contact_Frequency__C)));
      System.assert(Tsk.OwnerId == Acc.OwnerId);
      System.assert(Tsk.Subject == 'Follow Up ' + Acc.Name + '/' + Ctc.Name);
      System.assert(Tsk.WhoId == Acc.Primary_Contact__c);
      System.assert(Tsk.WhatId == Acc.Id);
      System.assert(Tsk.Priority =='High');          
          }
    }


Hello, I am fairly new to Salesforce and even more to APEX so I am having some difficulties in writing a Trigger.

So I have managed to create a list of tasks that have been completed. Now I want to create new tasks in x number of days but I don't know how to insert a list of new tasks. All tasks would have the same structure ('Follow Up' + Account/Contact Name; assigned to the same person has before OwnerId; Not Started, WhatId the same as before). There is just a few things I need the code to do.

ActivityDate should be: Today + Contact_Frequency__c (Contact Custom Field, number field)
WhoId should be: Primary_Contact__c (Account Custom Field, lookup to contact)
Priority should be dependent on a Tier__c (Account Custom Field, formula field): High if it is Tier 1, else normal

I would have done this myself but I still don't know how to reference other objects fields

Then I have another question regarding shared activities (how to create separate tasks for the participants of a given event), but that is for another day.

All help is appreciated
Hello, I am new to Salesforce, currently still with a trial version, and I am trying to create an Apex Trigger but before I get into it I wanted to ask if my idea is even possible. So I want a trigger that fires when a task is completed and creates a task the same account in (+) a specific time period. For the trigger part I want it to: (1) change the due date to the date it is completed; (2) schedule the next task depending on an account custom field (frequency); (3) assign it to a contact of that account, but only the one that is assigned as primary (custom field). Also the trigger needs to be built so it can handle mass updates of accounts/contacts/tasks.

I have tried to build something but still have very limited knowledge. If anyone has built anything similar and would like to share it with I would be extremely appreciated. Any help is welcome! Thanks