Skip to main content

Hi, Friends.

 

I want know if the challenge on Apex Trigger Bulk have someone problem. Because i proved of differents forms of code and not valide me challenge, much code that i proved equals to the much persons here . Even i proved with solution in Apex GitHub and neither validate me the challenge.

 

«An opportunity was created and expected to have a task associated with it, but it doesn't. Make sure your Apex trigger inserts the task into the database.»

 

My codes are the next:

 

1º code proved:

List<Task> taskList = new List<Task>();

for (Opportunity o :[SELECT Id,StageName FROM Opportunity WHERE StageName ='Closed Won' AND Id IN :Trigger.New]){

if(o.StageName == 'Closed Won'){

taskList.add(new task (Subject ='Follow Up Test Task' , WhatId=o.Id));

}

}

if(taskList.size() > 0){

insert taskList;

}

 

2º code proved:

List<Task> taskList = new List<Task>();

List<Opportunity> toProcess=null;

Switch on Trigger.operationType{

When After_Insert{

toProcess=Trigger.new;

}

When After_Update{

toProcess=[SELECT Id,StageName FROM Opportunity WHERE StageName ='Closed Won' AND Id IN :Trigger.New];

}

}

for (Opportunity o : toProcess){

if(o.StageName == 'Closed Won'){

taskList.add(new task (Subject ='Follow Up Test Task' , WhatId=o.Id));

}

}

if(taskList.size() > 0){

insert taskList;

}

Is all. Please ¿someone help me?

 

#Trailhead Challenges

16 réponses
  1. 29 janv. 2024, 19:39

    Had the same issue. I fixed it checking out the exact subject value in the log of debug section, because in my code I wrote it in english, but it seams the test run my code with the spanish value, due to the "Language & Time Zone" settings that I've got in my profile. It seams to be case sensitive as well. It worked well with the value of subject in spanish. This is what my code looks like:

     

    trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

        

        List<Opportunity> oppList = [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.New];

        List<Task> tasksToInsert = new List<Task>();

        

        for(Opportunity o : oppList){

            tasksToInsert.add(new Task(Subject='Tarea de prueba de seguimiento',//instead 'Follow Up Test Task'

                                       WhatId=o.Id));

        }

        if (tasksToInsert.size() > 0) {

            insert tasksToInsert;

        }

    }