You need to sign in to do that
Don't have an account?
cyberdynebot
Help with Bulkifying a Task Trigger
Fairly new to Apex and I have a trigger that checks if a Task is on a Contact and pulls in 2 fields from the Contact record that will update the Task.
It works fine for one record at a time but when trying to bulk if there are multiple tasks for the same Contact it will only fire for the last Activity in the Trigger.new list and not all that share the same WhoId.
Looks like the Map is only capturing the Contact Id once and then the SOQL query only returns one result. Just need help figuring out the solution so that each Task will get updated with the Contact fields.
It works fine for one record at a time but when trying to bulk if there are multiple tasks for the same Contact it will only fire for the last Activity in the Trigger.new list and not all that share the same WhoId.
Looks like the Map is only capturing the Contact Id once and then the SOQL query only returns one result. Just need help figuring out the solution so that each Task will get updated with the Contact fields.
trigger TaskTrigger on Task (before insert, before update) { Map<Id, List<Task>> conIdsMap = new Map<Id, List<Task>>(); for (Task t : Trigger.new) { //Search for Contact on Task if (t.whoid != null && t.whoid.getsObjectType().getDescribe().getName() == 'Contact') { List<Task> temp = new List<Task>(); temp.add(t); conIdsMap.put(t.whoid, temp); } } if (conIdsMap.size() > 0) { for(Contact con : [Select Id, Name, Status__c, Corp_Lead_Score__c from Contact where Id in :conIdsMap.keySet()]) { for(Task t :conIdsMap.get(con.Id)){ t.Stage__c = con.Status__c; t.Corp_Lead_Score__c = con.Corp_Lead_Score__c; } } } }
All Answers
see: ShowBox (https://showbox.software/) Lucky Patcher (https://luckypatcher.pro/) Kodi (https://kodi.software/)