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
Jakson MonteiroJakson Monteiro 

Trailhead: Bulk Apex Trigger

Unable to complete the challenge for the Apex Trigger using Bulk
error:Executing against the trigger does not work as expected.

Trigger code:

trigger ClosedOpportunityTrigger on Opportunity (before insert,before update)
{
  List <task> taskinsert= new List<task>();
   for(Opportunity o: Trigger.new)
    {
     if(o.StageName=='Closed Won')
     {
         Task t= new Task();
         t.Subject='Follow up Task';
        t.whatid=o.id;
taskinsert.add(t);
          
     }
        

      
    }
    insert taskinsert;
    
}
KaranrajKaranraj
Read the challenge descirption carefully. You must have class name or task name or record name as mentioned in the challenge description else the submission will fail.This will be applicable for all challenges in trailhead.

To complete this challenge, you need to add a trigger for Opportunity. The trigger will add a task to any opportunity inserted or updated with the stage of 'Closed Won'. The task's subject must be 'Follow Up Test Task'.

Thanks,
Karanraj
http://www.karanrajs.com
Pierre-AlainPierre-Alain
Hi Karanraj - I came accross the same error message and I double check all the names. My code is below. Am I missing anything ? thanks.

trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
  List<Task> taskList = new List<Task>();

    //If an opportunity is inserted or updated with a stage of 'Closed Won'
    // add a task created with the subject 'Follow Up Test Task'.
    for (Opportunity opp : [SELECT Id,Name FROM Opportunity
                     WHERE Id IN :Trigger.new AND StageName = 'Closed Won']) {
       //add a task with subject 'Follow Up Test Task'.
       taskList.add(new Task(Subject='Follow Up Test Task', WhatId = opp.id ));                
  }
                          
    if (taskList.size() > 0) {
        insert taskList;
    }
}
Andrea IanniAndrea Ianni
Try with this.
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {


	List<Task> listaTask = new List<Task>();
	
	for(Opportunity o: Trigger.new){ 
		if(o.stageName == 'Closed Won'){
			Task nuovoTask = new Task(Subject	='Follow Up Test Task', 
										WhatId	=o.Id);
			listaTask.add(nuovoTask);
		}
	} //endFor
	
	insert listaTask;
}


Jackson, I think your problem is at the beginning. You use before insert, before update, but you need to create the task after an Opportunity is created or updated. Otherwise you don't have an Id to connect to.
Francis Alberto Vargas CruzFrancis Alberto Vargas Cruz
Greetings:

I have been having a similar problem and no matter what I do, I cannot fix it. Does anyone knows what I'm doing wrong? Any help is apreciatted:
User-added image
Daniel ShaheenDaniel Shaheen
There is a filed on your opportunity object named "Discount Percent" is required.
Either you reflect that in your trigger or make the field not required.
Suresh Babu KattaSuresh Babu Katta
Andrea Ianni's code worked for me . Thanks to Andrea Ianni.
I just added size check to his code as below:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
	//...
    
    if(listaTask.size() > 0) {
        insert listaTask;
    }
}