+ Start a Discussion
Andrew EversleyAndrew Eversley 

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

Best Answer chosen by Andrew Eversley
Jai ChaturvediJai Chaturvedi
Hi Andrew,

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

Jai ChaturvediJai Chaturvedi
Hi Andrew,

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
This was selected as the best answer
Andrew EversleyAndrew Eversley
Thanx Jai, I had a custom field on Opportunity called Discount Percent that had the "Always require a value in this field in order to save a record" option chosen as default. I unchecked it so that it didn't always require a value and the challenge succeeded. Thanx for your quick and informative response.
Jai ChaturvediJai Chaturvedi
Welcome. 

Cheers.
Zach BakerZach Baker
Ahh, just ran across a similar error while trying to complete the Challenge on the "Using Numbers, Currency and Percentages in Formula" Module - unchecking the "Always require a value..." on the Discount Percent field allowed the challenge to successfully authenticate my work.  Thanks Jai and Andrew!  

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]
Punit ChoudharyPunit Choudhary
"Andrew Eversley" our suggestion is right is to unchecked the "Always require a value in this field in order to save a record"
James SinorJames Sinor
Thank you for this! Helpful when I ran into the same error today. 
Nidhi SoniNidhi Soni
Hi
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."
Orchay NaehOrchay Naeh
hello im a beginer to this platform, in trainig fot a developer position

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
 
Judith FrancisJudith Francis
Just make the Discount_Percent__c field not mandatory feild  by editing it in setup - object manager -  opportunity - Fields & Relationships. Edit the field and uncheck this option (Always require a value in this field in order to save a record). Or in the trigger while adding the new opportunity add the Discount Percent feild as well. 
Kohei SaitoKohei Saito
Thank you for this! I ran across this error today. However, this is so helpful that I could resolve it.