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
Punima SharmaPunima Sharma 

Apex Trigger

Hi, 
I am not able to complete Trailhead:Bulk Apex Triggers challenge ,I am facing  issue ' Challenge not yet complete... here's what's wrong: 
                                               Executing against the trigger does not work as expected'.  Debug logs are also not descriptive like error message.

Code :

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

    List<Task> tasks = new List<Task>();
     
    for (Opportunity opp: Trigger.New) {
        If (Trigger.isUpdate){
        if(opp.StageName!=trigger.oldMap.get(opp.Id).stageName && opp.StageName == 'Closed Won'){
            tasks.add(new Task(Subject = 'Follow Up Test Task',WhatId = opp.Id)) ;
        }
    } 
   
    If (Trigger.isInsert){
        if(opp.StageName == 'Closed Won'){
            tasks.add(new Task(Subject = 'Follow Up Test Task',WhatId = opp.Id)) ;
        } 
    } 
}    
    if (tasks.size()< 0){
        insert tasks;
    }
}
I have no workflow,flow or powerbuilder process related to opportunity.

P.S.
I have followed suggestion of other users (Chris Edwards and Sandeep Bhanot) which they have posted  in a similar post but in my case this issue is not fixed.
 
Best Answer chosen by Punima Sharma
CyberJusCyberJus
In your last section you have:

 if (tasks.size()< 0) {

This should be tasks.size()>0 meaning the size of the list is greater than 0. 

All Answers

CyberJusCyberJus
In your last section you have:

 if (tasks.size()< 0) {

This should be tasks.size()>0 meaning the size of the list is greater than 0. 
This was selected as the best answer
kaustav goswamikaustav goswami
Problem may be here :
if (tasks.size()< 0){
        insert tasks;
    }

It should not be tasks.size() < 0 it should be tasks.size() > 0. A slight oversight i think.

Thanks,
Kaustav
Punima SharmaPunima Sharma
Thanks Cyber Jus and Kaustav........Yes insert should be made only when any task size is created coz of insert of update. My bad.