+ Start a Discussion
SFWesSFWes 

Trigger not working properly when executed from WorkFlow

I've created the following Trigger below. This Trigger works as expected when manually creating records, but for some reason when it's executed because of Workflow, it doesn't work. It also seems as if the Trigger is being executed multiple times. I put some System.Debug lines in to see where the code is and isn't executing and I noticed that some of the debug lines were being executed multiple times. Am I doing something wrong?

 

Here is how it should work - When a specific field on the Opp is altered, a workflow is triggered which creates a new Task. When the Task is inserted, then this Triggered executes. This Trigger is supposed to check if the owner of the Task is the same owner of the Account that the Opportunity is tied to. If they are the same, then a field on the Task is updated. 

 

Like is said, if I manually make this Task then the Trigger works as intended. It even works when I make updates to the Task, like change the owner or Record Type, but it doesn't work when triggered from workflow.

 

Any help would be greatly appreciated.

 

 

This was really my first Trigger learning about not exceeding govern limits, so my apologies if the code isn't all that great.

 

Trigger TaskandAccountOwnerMatch on Task (after insert, after update) {
    
    List<Task> tasks = new List<Task>(); //Tasks to Update
    Map<Id, Task> taskMap = new Map<Id, Task>();//Task & Opportunity Mapping
    Map<id, id> oppAccMap = new Map<id, id>(); //Account & Opportunity Mapping
    Map<Account, Task> accTaskMap = new Map<Account, Task>(); //Account & Task Mapping
    Id rtTaskID = [select Id from RecordType where name = 'Standard Task' and SObjectType = 'Task' limit 1].Id;
    
    
    //Loop through the Trigger
    for(Task tsk : Trigger.new){
        
        //This code should only apply only to one Task Record Type
        if(tsk.RecordTypeId == rtTaskId){
            //Ensure there is a value provided and that it's for an Opportunity
            if(tsk.WhatId != null && string.valueOf(tsk.WhatId).startsWith('006')){
                if(Trigger.isInsert ){ 
                    taskMap.put(tsk.WhatId, tsk); //Add to Mapping
                }
                else if (Trigger.isUpdate){
                    //To avoid recursion (govern limits), we check to see if the fields have actually changed.
                    if(Trigger.oldMap.get(tsk.id).ownerId != tsk.ownerid || Trigger.oldMap.get(tsk.id).WhatId != tsk.WhatId){
                        taskMap.put(tsk.WhatId, tsk); //Add to Mapping
                    }
                }
            }else{//No value provided or it's not an Opportunity
                if (Trigger.isUpdate){
                    //If the related object has changed or the checkbox is true
                    if(Trigger.oldMap.get(tsk.id).WhatId != tsk.WhatId || tsk.Task_and_Account_Owner_Match__c == True
                      || Trigger.oldMap.get(tsk.id).RecordTypeId != tsk.RecordTypeId){
                        tasks.add(new Task(id = tsk.id, Task_and_Account_Owner_Match__c = False));
                    }
                }
            }
        }
    }
    
    //Only move forward if the taskMap has values
    if(taskMap.size() > 0){
        //Map AccountID with OpportunityID
        for(Opportunity opp : [select Id, AccountId from Opportunity where Id in : taskMap.keySet()]){
            oppAccMap.put(opp.AccountId, opp.Id); //Add the AcountID and OpportunityId to the Map
        }
    
        //Map Account to Task
        for(Account acc : [select id, Name, OwnerId from Account where Id in : oppAccMap.keySet()]){
            accTaskMap.put(acc, taskMap.get(oppAccMap.get(acc.id)));    
        }
    
        for(Account accTmp : accTaskMap.keySet()){
            //Assume the Owners don't match
            Boolean isMatch = True;
            //Check if Account owner and Task owner match
            if (accTmp.OwnerId == ((Task)accTaskMap.get(accTmp)).OwnerId){
                isMatch = True;
            }

            tasks.add(new Task(id = accTaskMap.get(accTmp).id, Task_and_Account_Owner_Match__c = isMatch));
        }
    }
    
    //Proceed with update if tasks size is greater then 0
    if (tasks.size() > 0){
        update tasks;
    }
}