I have a lookup on Teams called Managers (User Lookup).

When a task related to Teams is completed, I want to update a field on tasks called match and

also update a field on Teams called completed = true if the Assigned to on the task == the manager on teams.

I’m stuck since Im not exactly sure how to match tasks assigned to== manager field.

Should I set a variable to = my manager Id then query out a list of tasks to see if the assigned to field matches that variable? Then how would I include it in my IF statement?

And how do I update task and team? This will only update teams? I think I need to query tasks where the whatId ==teams as well?

I don’t think I should actually reference Team__c but the whatIds for tasks? Ive never written anything with tasks so I have no idea how the relationships work or how it should. I think I need to scratch everything and start over.

2 Answers 2


I think here you need to divide your code into two parts(Assuming you have a trigger on Task object):

Before Update trigger part:

Here you will grab all the tasks which are

  1. Related to Teams and whose
  2. status changed to complete

Then Update Match field of task.

Note: Before triggers are generally used to set the fields of same object.

After Update Trigger part

Here again you will grab tasks which are

  1. Related to Teams and whose
  2. status changed to complete and
  3. assignedToId == Team.managerId

Then you will make a list of all these teams, and update Completed field of team.

Note: After triggers are generally used to set the fields of other object or to create or update records of other objects.

There is a Trigger module on Trailhead which you can see also following link contain some patterns to write triggers.

Trigger Frameworks and Apex Trigger Best Practices

  • 1
    I have updated my answer with some useful links which i think you should look. Then please update your code with the changes you have made and specify exactly what issue you are facing. It is really simple that's why i am not writing entire trigger for you.
    – Mr.Frodo
    Commented May 27, 2018 at 6:53

Your code should be more or less like this

    public static void checkIfTaskCompleted (Map<Id, Task> newTasks, Map<Id, Task> oldTasks){
    List<Team__c> teamsTobeUpdated = new List<Team__c>();
    Set<Id> teamIds = new Set<Id>();
    Set<Id> managerIds = new Set<Id>();
    Map<Id, Id> teamToAssignToMap = new Map<Id, Id>();
    for(Task t : newTasks.values()){
        // collect all the tasks whose status is changed to completed
        if(t.Status != oldTasks.get(t.Id).Status && t.status == 'Completed'){
            // collect all the team ids whose tasks are modified
            // collect all the task's assigned tos to query on teams
            // collect team id to assigned to mapping to be used for later processing
            teamToAssignToMap.put(t.whatId, t.ownerId);
    // query for teams whose tasks are modified and manager is on one of the task's assigned to
    Map<Id, Team__c> teamMap = new Map<Id, Team__c>([SELECT Id, Manager__c FROM Team__c WHERE Id IN :teamIds AND Manager__c IN :managerIds LIMIT 50000]);
    for(Task t : newTasks.values()){
        // verify that task assigned to is same as manager on the team
        if(teamMap.containsKey(t.whatId) && t.ownerId == teamMap.get(t.whatId).Manager__c){
            // you don't need to exclusively update tasks as you are modifying the values in newer version of the records
            t.match__c = true;
            Team__c team = new Team__c(Id = t.whatId, Completed__c = true);
    // you can do this or database.update as per your desire
    update teamsTobeUpdated;
  • You mean manager field is looking up Account instead of User?
    – Suneel
    Commented May 31, 2018 at 1:15

You must log in to answer this question.

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