0

i'm receiving lead records via API REST from an external service. If the record doesn't accomplish with the condition, i'm adding an addError() to the lead record. The problem is that i can't control the allOrNone since i don't have any previous class where i can set allOrNone to False, so if one record fails, the other 99 will fail too.

Do you know any way to control that, via addError() or another solution?

My code is as follows:

public with sharing class LeadTriggerHandler implements ITrigger{

public void bulkBefore() {

if((Trigger.isInsert && !Trigger.isDelete) || Trigger.isUpdate){

        for(Lead l : (List<Lead>)Trigger.new) {
            if(l.param1 == null || (l.param2 == null && l.param3 == null)){
                l.addError('Insertion error. Param 1 and param2 or param3 can't be null');
            }else{
             ...
         }
    }
}

1 Answer 1

1

You can't control allOrNone mid-transaction. It is the caller's responsibility to set the flag appropriately. The best you can do would be to delete the record in an after-insert trigger:

// Note: Pseudo-Code, fill in missing pieces as necessary
trigger afterInsertLead on Lead (after insert) {
  Lead[] deletions = new Lead[0];
  for(Lead record: Trigger.new) {
    if(doesNotMeetCriteria(record)) {
      deletions.add(record);
    }
  }
  delete deletions;
}

Note that here, you will not return an error to the caller, but instead simply delete the record instead. This behavior is usually undesirable, but you don't really have any other option if you can't control the caller's API settings.

If you do have access to the caller, set the allOrNone flag. There are options to do so in REST and SOAP, it's a simple matter to set the appropriate attribute.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .