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
bbeth soutullobbeth soutullo 

Apex BulkTriggers question

Hello Community! Hope im finding you all doing alright.
Im studying apex and im kind of stuck in a Trail about bulk triggers.
I've found the answer to the challenge in another post but i want to know what is wrong with my approach since i dont seem to understand, would someone explain what is wrong with my code?
i've wrote the following:


Create a bulkified Apex trigger that adds a follow up task to an opportunity if its stage is Closed Won. Fire the trigger after inserting or updating an opportunity.
Name: ClosedOpportunityTrigger
Object: Opportunity
Events: after insert and after update
Condition: Stage is Closed Won
Operation: Create a task:
Subject: Follow Up Test Task
WhatId = the opportunity ID (associates the task with the opportunity)

 
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    List<Opportunity> oppIDs = [SELECT Id FROM Opportunity WHERE id IN :Trigger.New];
    for (Opportunity opp: oppIDs){
        if (opp.StageName == 'Closed Won'){
            Task newTask = new Task();
            newTask.Subject = 'Follow Up Test Task';
			newTask.WhatId = opp.Id;
            insert newTask;
        }
    }

Thank you so much in advance!
AbhinavAbhinav (Salesforce Developers) 
Hi Bbeth,

Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.

Thanks!
bbeth soutullobbeth soutullo
Hi Abhinav! I wasn't asking for the solution (as i've already seen it) I was asking if someone could explain to me what is wrong with my code (it is very different from the solution but i don't understand what is wrong with mine
bhanu prakash 350bhanu prakash 350
Hi soutullo,

have a glance at the below code.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
    //List<Opportunity> oppIDs = [SELECT Id FROM Opportunity WHERE id IN :Trigger.New];
    List<Task> taskList=new List<Task>();
    for (Opportunity opp: Trigger.new){
        if (opp.StageName == 'Closed Won'){
            Task newTask = new Task();
            newTask.Subject = 'Follow Up Test Task';
			newTask.WhatId = opp.Id;
           // insert newTask;
           taskList.add(newTask);
        }
    }
    insert taskList;
}
1. no need to query and store the same data into another variable, Because wasting memory.
2. Bulk trigger means to handle a single record as well as a list of records at a time. So don't insert every single record on iteration.
3. In the best practices don't perform DML operations on loops.

I hope you understand the explination.