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
Koti P 6Koti P 6 

Getting error while executing the TrailHead Challenge for Bulk Trigger

Hi Friends,

I have written the below code for TrailHead Challenge and getting the below error: Can some one sugget what is going wrong
Error:
-------------
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]

Code:
------------
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update)
{
         List<Task> tt = new List<Task>();
    
           
    for (Opportunity op : [select Id, Name, OwnerId from Opportunity where Id IN :Trigger.new and StageName='Closed Won'])
    {
       
        Task t = new Task(WhatId = op.Id, Subject = 'Follow Up Test Task', OwnerId = op.OwnerId);
        
        tt.add(t);
       
    }
    
    if (tt.size()>0)
    {
        insert tt;
    }
    
}

Regards,
Koti
Best Answer chosen by Koti P 6
Steven NsubugaSteven Nsubuga
There is field Discount_Percent__c which requires value while creating/updating records. Just provide the value to this field in your code and it will work.

All Answers

Steven NsubugaSteven Nsubuga
There is field Discount_Percent__c which requires value while creating/updating records. Just provide the value to this field in your code and it will work.
This was selected as the best answer
Steven NsubugaSteven Nsubuga
Alternatively, make sure that Discount_Percent__c is not a required field on the Opportunity object,or you can assign it a default value.
Koti P 6Koti P 6
Thank you so much Steven. But one thing iam wondering, there are other fields in the Opportunity object are required fileds like CloseDate, but why the error is thrown only for Discount_Percent__c filed. it's just out of curiosity iam asking this question. 
Steven NsubugaSteven Nsubuga
If a required field has a default value defined then all is well. Also, some other processes may populate the required fields. 
Koti P 6Koti P 6
Got it Steven, thanks for the prompt response.