I've been using addError() to prevent dml insertions of records that don't accomplish with the validation rule. The problem is that using that, if i insert a list of 100 records and one fails, the other 99 doesn't get inserted because i receive the next exception:
EXCEPTION_THROWN|[24]|System.DmlException: Insert failed. First exception on row 1; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION
There is any way to catch the exception ort an alternative that allows me to insert all the records that don't fail?
Here is the code:
public void bulkBefore() {
if((Trigger.isInsert && !Trigger.isDelete) || Trigger.isUpdate){
for(GPMIR_obj_energyLead__c l : (List<GPMIR_obj_energyLead__c>)Trigger.new) {
if(l.GPMIR_fld_codigoOrigen__c == null || (l.GPMIR_fld_phone__c == null && l.GPMIR_fld_email__c == null)){
l.addError('Error en la inserción. El origen, el canal y el teléfono o Email tienen que estar informados');
lstLeadDigitalRecIncom.add(l);
}else{...}
public void beforeInsert(SObject so) {
String errorMessage;
GPMIR_obj_energyLead__c eLead = (GPMIR_obj_energyLead__c) so;
if(lstLeadDigital.contains(eLead)){
if(lstLeadDigitalRecIncom.isEmpty() || !lstLeadDigitalRecIncom.contains(eLead)){
eLead.RecordTypeId = rtDigitalId;
if(eLead.GPMIR_fld_generationDate__c == null){
eLead.GPMIR_fld_generationDate__c = Date.today();
}
lstSobjLeadDig.add((sObject)eLead);
}else{
eLead.RecordTypeId = rtDigitalId;
}
}else{...}
I would really appreciate any help :)