notthehoff
notthehoff

Reputation: 1242

What is wrong with my code on Trailhead challenge Developer Beginner > Apex Triggers > Bulk Apex Triggers

This is the challenge:

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'.

Here is my Code

trigger ClosedOpportunityTrigger on Opportunity (before insert, before update) {
List<Opportunity> opportunities = [SELECT Id, StageName
                                   FROM Opportunity
                                   WHERE Id
                                   IN :Trigger.New];
List<Task> tasksToUpdate = new List<Task>();
System.debug('##### OPS' + opportunities);
for(Opportunity o : opportunities){
    System.debug('##### ' + o.StageName);
    if(o.StageName == 'Closed Won'){
        Task thisTask = new Task(WhatId = o.Id, Subject = 'Follow Up Test Task');
        tasksToUpdate.add(thisTask);
        System.debug('##### ' + tasksToUpdate);
    }
}
insert tasksToUpdate;

}

When I try to validate through trailhead, it gives a "Challenge Not yet complete... here's what's wrong: Executing against the trigger does not work as expected." error.

I added some debug print and it seems to show that the soql statement just does not pull any results, so it does not enter the if statement. It seems a pretty straightforward soql statement to me, but I must be missing something. This happens no matter if I add or update an item.

Thanks in advance

Upvotes: 2

Views: 8727

Answers (6)

Ranveer
Ranveer

Reputation: 1

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

List oppty = [Select id from opportunity where id IN :Trigger.New AND StageName = 'Closed Won'];

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

for (Opportunity opp : oppty){

    Task thistask = new Task (Subject = 'Follow Up Test Task',WhatId = opp.ID);
    new_task.add(thistask);
}

insert new_task;

}

Upvotes: 0

narapanaidupulipati
narapanaidupulipati

Reputation: 11

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

        for(Opportunity opp : [Select Id,StageName from Opportunity 
                                  where Id in :Trigger.New AND StageName = 'Closed Won'])
        {
                taskListtoInsert.add(new Task(Subject ='Follow Up Test Task',WhatId = opp.Id));

        }

    if(taskListtoInsert.size()>0)
    {
        insert taskListtoInsert;
    }
}

Upvotes: 1

Vipul
Vipul

Reputation: 1

Here's code that worked:

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

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

// Iterate over opportunities that are in this trigger and have a stage of "Closed Won"
for (Opportunity op: [SELECT id FROM Opportunity 
                      WHERE Id IN :Trigger.New AND
                      StageName =: 'Closed Won']) {

                          if ((Trigger.IsUpdate && Trigger.oldMap.get(op.Id).StageName != 'Closed Won')) OR 
                              (Trigger.IsInsert) {
                                  OpTaskList.add(new Task (Subject='Follow Up Test Task', 
                                                           WhatId = op.Id)); }          
                      }

If (OpTaskList.size() > 0) { 
    Insert OpTaskList ;
}   

}

Upvotes: 0

Julien Souchi
Julien Souchi

Reputation: 11

All right, I was having trouble with this challenge because of the Task things, seems it's just a default object from salesforce!

Your problem is that you don't bulk enough your code as they shown in the trail.

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

    List<Opportunity> closedOpp = [Select id from opportunity
                                  where id IN :Trigger.New
                                  AND StageName = 'Closed Won'];

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

    for (Opportunity o : closedOpp){

        Task tache = new Task (Subject = 'Follow Up Test Task',
                              WhatId = o.ID);
        triggeredTasks.add(tache);
    }

    insert triggeredTasks;
}

Upvotes: 0

Edmondo
Edmondo

Reputation: 20090

The problem with your code is that you are running on before insert/update where the element has no ID yet. Therefore the following code:

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

Insert a task with an empty WhatId which doesn't pass the test. Just change to after insert/after update the trigger event.

Upvotes: 0

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

In a Trigger context you do not need to use a SOQL query to retrieve the records that are being inserted or updated.

Also, with a before insert trigger the records won't be in the database yet, so they won't have any Id's defined. That's why the query isn't returning anything.

Instead you will want to use the Trigger Context Variables to work the the records.

In particular, you can use Trigger.new to get a collection of records being inserted or updated. Try looping over this collection instead of using a SOQL query in the trigger.

Upvotes: 1

Related Questions