function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cyberdynebotcyberdynebot 

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.
 
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;
                }
            }  
        }
}

 
Best Answer chosen by cyberdynebot
Alain CabonAlain Cabon
Hi,
 
    for (Task t : Trigger.new) {
        //Search for Contact on Task
        if (t.whoid != null && t.whoid.getsObjectType().getDescribe().getName() == 'Contact') {
            if (conIdsMap.containsKey(t.whoid)) {
                List<Task> temp = (List<Task>)conIdsMap.get(t.whoid);
                temp.add(t);
            } else {
                List<Task> temp = new List<Task>();
                temp.add(t);
                conIdsMap.put(t.whoid, temp);
            }          
        }
    }

 

All Answers

Alain CabonAlain Cabon
Hi,
 
    for (Task t : Trigger.new) {
        //Search for Contact on Task
        if (t.whoid != null && t.whoid.getsObjectType().getDescribe().getName() == 'Contact') {
            if (conIdsMap.containsKey(t.whoid)) {
                List<Task> temp = (List<Task>)conIdsMap.get(t.whoid);
                temp.add(t);
            } else {
                List<Task> temp = new List<Task>();
                temp.add(t);
                conIdsMap.put(t.whoid, temp);
            }          
        }
    }

 
This was selected as the best answer
cyberdynebotcyberdynebot
Thank you!
nazim zmirlinazim zmirli
I have created an Apex Trigger that works exactly how I need it to within the sandbox. However, when I created the Apex Class and ran the test, I get a failing grade and am not sure how to troubleshoot my code.
see: ShowBox (https://showbox.software/) Lucky Patcher (https://luckypatcher.pro/) Kodi (https://kodi.software/)
Lionel Messi 13Lionel Messi 13
There is an option for everyone to use the phone, install lucky patcher download (https://techbigs.com/lucky-patcher.html) to use the mod apk (https://techbigs.com) techbigs features.
amina asiaamina asia
I am also new to the apex and struggling to work on the app called fmwhatsapp (https://gbmod.net/fmwhatsapp-2/), would have been better if they had proper tutorials for each step.
John Wick 83John Wick 83
There is an option for everyone to use the phone, install lucky patcher download (https://gbwamod.com/download-fmwhatsapp-apk/) to use the mod apk (https://gbwamod.com) techbigs features. Thanks.