I wonder how to remove an item from insertion.
I want to avoid the insertion of contact with lastName = 'B' but insert the others, How can I do it?, adding an error with
record.addError
is a "all or none" so there are no way to avoid the insertion of a single record,
Trigger.New.remove(1);
doesn't work because it is a only read collection (and it makes very sense because there is also a Trigger.NewMap referencing the same item)
I will place some code because I think is the best way to explain:
Trigger on Contact
trigger AvoidB on Contact (before insert) {
for(Contact c : Trigger.new){
if(c.lastName == 'B'){
//c.addError('Is a B');
Trigger.New.remove(1);
}
}
}
Execute console
List<Contact> c = new List<Contact>();
c.add(new Contact(lastName = 'A'));
c.add(new Contact(lastName = 'B'));
c.add(new Contact(lastName = 'A'));
c.add(new Contact(lastName = 'A'));
c.add(new Contact(lastName = 'A'));
insert c;