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
Alexis ScottAlexis Scott 

Bulk Apex Triggers Challenge- DmlException

Hello! 

I am working on the challenge for the BulkApexTriggers section and I keep receiving the following error. I am not sure what is wrong with my system. I have a Discount Percentage field under Accounts and Opportunities and that field is not required for the challenge. I received this error message for a lot of triggers I have tried to imput, and I'm not really sure what is going on. 

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 the Trigger I have entered into my Developer Console. 
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {

    List<Task> taskList = new List<Task>();
    
    for (Opportunity opp : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Closed Won' AND Id IN :Trigger.new]){
                    
            taskList.add(new Task(Subject = 'Follow Up Test Task',
                                  WhatId = opp.Id));
       
    }

    if(taskList.size()>0){
        
        insert taskList;
        
    }
    
}

Thanks for your help! 
Best Answer chosen by Alexis Scott
Jason FlippenJason Flippen
Hi Alexis,

It looks like you have a field (Discount_Percent__c) on the Task object that is required, so you need to add that when creating a new Task record.  So change line 7 of your code to this...

taskList.add(new Task(Subject = 'Follow Up Test Task',
                                     Discount_Percent__c = 0,
                                     WhatId = opp.Id));

If you have an actual Discount Percentage you can use that, but in my example I'm just setting it to 0.

I hope this helps.

Jason

All Answers

Jason FlippenJason Flippen
Hi Alexis,

It looks like you have a field (Discount_Percent__c) on the Task object that is required, so you need to add that when creating a new Task record.  So change line 7 of your code to this...

taskList.add(new Task(Subject = 'Follow Up Test Task',
                                     Discount_Percent__c = 0,
                                     WhatId = opp.Id));

If you have an actual Discount Percentage you can use that, but in my example I'm just setting it to 0.

I hope this helps.

Jason
This was selected as the best answer
sfdcMonkey.comsfdcMonkey.com
hi scott
There is field Discount_Percent__c on  Opportunity which requires value while creating/updating records. Just provide the value to this field in your code and it will work. OR  find field on opportuunity object and edit it and uncheck REQUIRED chekcbox and
try again
Thanks
let me inform if it work and
Mark it best answer if it helps you so it make proper solution for others :)