You need to sign in to do that
Don't have an account?

Bulk Apex Triggers Trailhead Error
Hello Community,
Having an issue with a Bulk Apex Trigger challenge here, hope you guys can assist.
Here is the critieria:
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'.The Apex trigger must be called 'ClosedOpportunityTrigger'
With 'ClosedOpportunityTrigger' active, if an opportunity is inserted or updated with a stage of 'Closed Won', it will have a task created with the subject 'Follow Up Test Task'.
To associate the task with the opportunity, fill the 'WhatId' field with the opportunity ID.
This challenge specifically tests 200 records in one operation.
Here is the Error Received:
Challenge not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Discount_Percent__c]: [Discount_Percent__c]
Here Is My Code:
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList = new List<Task>();
for(Opportunity opp : Trigger.new) {
//Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
if(Trigger.isInsert) {
if(Opp.StageName == 'Closed Won') {
taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
}
}
//Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
if(Trigger.isUpdate) {
if(Opp.StageName == 'Closed Won'
&& Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
}
}
}
if(taskList.size()>0) {
insert taskList;
}
}
Please Note: I appended the code from SumitKumar from another post which worked for others as I changed my code to suit the suggested code.above. It seems I may have an insert dml exception issue maybe caused by an earlier code insertion. PLEASE ADVISE
There is field Discount_Percent__c on Task or Opportunity which requires value while creating/updating records. Just provide the value to this field in your code and it will work.
Please mark this solution if it helps out.
Thanks
All Answers
There is field Discount_Percent__c on Task or Opportunity which requires value while creating/updating records. Just provide the value to this field in your code and it will work.
Please mark this solution if it helps out.
Thanks
Cheers.
Error message I received:
Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Discount_Percent__c]: [Discount_Percent__c]
I am doing securtiy specailst super badge step 4.
Do I need any coding for that if not then how can i solve this ?
Any change in the Amount field on opportunity records must be recorded.
can someone explain me this ?
Here is the error
"Challenge Not yet complete... here's what's wrong:
The Opportunity Amount field does not appear to be tracking field value changes."
i solved a bit diffrently so hopefully it will help to whom ever get stuck on this challenge
1) go to oppertunity and set a default value of discount presentage, i used 0 but i i guess anything will do.
2) this is the code i wrote:
trigger ClosedOpportunityTrigger on Opportunity ( after insert, after update)
{
List<Task> tasks = new List<Task>();
for (Opportunity op : Trigger.New)
{
if(op.StageName == 'Closed Won')
{
tasks.add(new Task(WhatId=op.Id, Subject = 'Follow Up Test Task'));
}
}
if(tasks.size() > 0)
{
insert tasks;
}
}
***
i tried it without the list of tasks but found out there are 151 records to insert witch exceeds the maximum allowable queries.... just delete one geez... no one will miss it lol, so anyway collected them to 1 list as suggested here and insert them as one unit.... solved it thank you Andrew,
didn't understand y to devide the trigger inside to after inserts and after update as i seen in more then 1 solutions for this problems would love to know the reason.
best regards
Naeh Orchay
CloudTech Ra'ánana Israel
orchay.n@cloudtech-apps.com
List<Task> ListTask = new List<Task>();
for(Opportunity opp : Trigger.New ){
if(opp.StageName == 'Closed Won'){
Task a = new Task();
a.subject = 'Follow Up Test Task';
a.whatId = opp.Id;
ListTask.add(a);
}
}
if(ListTask.size()>0)
{
insert(ListTask);
}
}
I am a beginner to this platform. I am facing a problem in completing the challenge in module 'Bulk Apex Triggers'. I am continuing with the thread which is more than 5 years old as I am facing a similar problem in the same module.
I have created the trigger. When I manually create or update an Opportunity record with a stage of 'Closed Won', the Apex trigger is creating a Task. So it is working as expected. But when I click on "Check Challenge to earn 500 points" I am getting following error -
Challenge not yet complete in My Trailhead Playground 1
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClosedOpportunityTrigger: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0 with id 00T2y00000GeCuaEAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id] Trigger.ClosedOpportunityTrigger: line 11, column 1: []
My code is
I tried to create a Task manually with only the two fields - Subject and WhatId. This is also working fine. Please help me to find out where it is going wrong.
Thanks in Advance.