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
Maxim PetrosovMaxim Petrosov 

System.DmlException Problem

trigger ClosedOpportunityTrigger on Opportunity (after insert) {

    List<Task> taskListToInsert = new List<Task>();
    
    for(Opportunity opp:Trigger.new)
    {
       if(opp.StageName == 'CloseWon') 
       {
           Task t = new Task();
           t.Subject = 'Follow Up Test task';
           t.WhatId = opp.Id;
           taskListToInsert.add(t);
       }      
    }
    
    if(taskListToInsert.size() > 0)
       {
          insert taskListToInsert; 
       } 
}
When I'm trying ti invoke this trigger I have this error

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]
Can anyone help me with this?
Derrick AbbeyDerrick Abbey
Hi Maxim,  That error indicates that you have a required field on the task (activity) object that is not being populated by this trigger.  You will need to provide a value in the Discount_Percent__c field in order to complete the insert.
Maxim PetrosovMaxim Petrosov
Hi Derrck. Thank you very much for your answer. Look at this photo. I pointed Requaried in General Options but still the same error. Help me please I'm stuck withthis challenge. 




User-added image
Derrick AbbeyDerrick Abbey
Hi Maxim,

How are you invoking the trigger? Is it in an Apex test class that you're getting the error?
Maxim PetrosovMaxim Petrosov
Hi Derrick,
Thanks a lot that you found a time to reply. I created this trigger for Opportunities to complete my challenge.
When I'm checking it System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Discount_Percent__c]: [Discount_Percent__c]
But I have already  this Discount_Percent__c field in Opportunity Custom Fields & Relationships. I don't know what I have to do.