Skip to main content

Module: Apex Triggers -> Bulk Apex Triggers

I'm trying to complete the challenge from the above module. I'm getting the following error when I check the challenge. 

Error: We created an opportunity and expected it to have an associated task, but it didn’t. Make sure your Apex trigger inserts the task into the database.

Getting this error, someone please help me out

As per the instruction, I've created the bulk apex trigger for Opportunity Object.

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

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

for(Opportunity opp: Trigger.New){

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

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

tasks.add(task);

}

}

if (tasks.size() > 0) {

insert tasks;

}

}

I've manually created an Opportunity with Stage Closed Win and I can see the follow-up task is getting created for the same.

2022-01-09 11_13_48-Clipboard.jpg

2022-01-09 11_15_34-Clipboard.jpg

8 件の回答
  1. 2022年1月9日 6:35

    @Keiji Otsubo, Thanks for your help! 

    It was an issue with a custom field setting in Opportunity Object, Discount_Percent__c in Opportunity Object was marked as the required field! I reverted it and it worked!

    Thanks a lot again for your effort! much appreciated :)

0/9000