You need to sign in to do that
Don't have an account?
bbeth 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)
Thank you so much in advance!
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!
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!
have a glance at the below code. 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.