0

I have a trigger that is triggered before update or before insert

for(CustomerProfile__c cp : listCustomerProfiles){
        if(...){
          cp.addError('A Customer Profile record has 
                       already been created for this brand.');
        }

And in the test class i am using only insert listOfRecord and not Database.insert(listOfRecords,false)

Data is imported using the dataloader or api so i cannot use Database.insert(listOfRecords,false) in order to bypass the records on which the addError() method is invoked. I know that by using addError() on 1 record will neglect the DML and do a roll back.

Is there anyway to only insert/update the records that don't meet the condition and for the records who meet the condition to NOT include them in the DML operation and also sending an error or message for each one?

2

The trigger can not control the allOrNone behavior. The caller decides which mode to use. By default, the Apex Data Loader uses allOrNone=false, and you can also specify this flag in any API call, too. Your unit test should be using Database.insert(records, false); to verify that some records can be inserted successfully.

2

Your understanding of how Data Loader interacts with addError is incorrect. Calling SObject.addError acts the same way as a Validation Rule would, failing just that particular record. If your insert operation allows for partial success, any records which have not had an error added will save.

1

The all or nothing is enforced only for the apex dml's where as When errors occur because of a bulk DML call that originates from the SOAP API with default settings, or if the allOrNone parameter of a Database DML method was specified as false, the runtime engine attempts at least a partial save

  1. During the first attempt, the runtime engine processes all records. Any record that generates an error due to issues such as validation rules or unique index violations is set aside.
  2. If there were errors during the first attempt, the runtime engine makes a second attempt that includes only those records that did not generate errors. All records that didn't generate an error during the first attempt are processed, and if any record generates an error (perhaps because of race conditions) it is also set aside.
  3. If there were additional errors during the second attempt, the runtime engine makes a third and final attempt which includes only those records that didn’t generate errors during the first and second attempts. If any record generates an error, the entire operation fails with the error message, “Too many batch retries in the presence of Apex triggers and partial failures.”

So addError() will technically work when using dataloader for bulk records as partial success insert or Database.insert rather than just insert.

Read Bulk DML Exception Handling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.