+ Start a Discussion
Irene SlessIrene Sless 

How do I achieve a partial insert on an insert trigger?

I have an insert/update trigger where I need to prevent a record of a certain type to be skipped over (not inserted) in a batch. I've read up and see I can do this using the addError method, but currently if I do that it rolls back the entire batch, even the valid ones. Furthermore I've read that it depends on the allOrNone parameter being set - I am setting it (even though it says the default setting = false), yet it still rolls back the entire batch. What am I doing wrong, or is this not possible to do? How can I achieve a partial insert?
trigger InvoiceLineBeforeInsertUpdate on InvoiceLine__c (before insert, before update) {
    
    Map<string, string> mapLineTyps = new Map<string, string>();
    Map<Id, string> mapNarratives = new Map<Id, string>();
    string strNarr;
    Id inv;
    Database.DMLOptions dlo = new Database.DMLOptions();
    dlo.OptAllOrNone = false;

    try{   
        for(InvoiceLine__c  invln : trigger.new){
            if(invln.LineType__c == 'Narrative'){
                if(inv != invln.Invoice__c){
                    mapNarratives.put(inv, strNarr);
                    inv = invln.Invoice__c;
                    strNarr = invln.Description__c;
                }
                else if(inv == invln.Invoice__c){
                    strNarr += + '\r\n' + invln.Description__c;
                }
                invln.addError('Exclude Narrative line: ' + invln.Description__c);
            }
                
        }
        if(mapNarratives.size() > 0){
            //add the last entry
            mapNarratives.put(inv, strNarr);
            List<Invoice__c> invoices = new List<Invoice__c>();
	        Invoice__c updInv;
            //Add the Narratives to the Invoice. Remove the blank entry in the map.
            mapNarratives.remove(null);
            for(Id invId : mapNarratives.keySet()){
                updInv = new Invoice__c(Id = invId, Description__c = mapNarratives.get(invId));
                invoices.add(updInv);
            }
            update invoices;
        }
        
    } catch(NullPointerException e){
        System.debug('##InvLineBF Catch Error: An error has occurred where there is no value for an ID: ' + e.getMessage());
        
    } catch(Exception e){
        System.debug('##InvLineBF Catch Error: The following error has occurred: ' + e.getMessage());
    }
    
}