You need to sign in to do that
Don't have an account?
ZANE PRATER
Database.SaveResult[] not working for bulk updates. I am trying to store the saved results so that each record is handled appropriately but what is happening is when one record fails then all the rest fail to update in that batch.
Here is the code and trigger works fine with a single update but fails the whole batch when one fails.
trigger AgreementTrigger on Apttus__APTS_Agreement__c (after update, before update) { set<Id> agmts = new set<Id>(); set<Id> agmts2 = new set<Id>(); set<Id> pbuns = new set<Id>(); List<Product_Bundle__c > pbs = [Select Id From Product_Bundle__c Where Bundle_Type__c = 'Service Pack']; For(Product_Bundle__c pb : pbs) If(pbs.size() > 0) { pbuns.add(pb.Id); system.debug('^^^^^^^^^^^^^^'+pbuns); } List<Apttus__AgreementLineItem__c> lines = new list<Apttus__AgreementLineItem__c>(); List<Apttus__AgreementLineItem__c> lines2 = new list<Apttus__AgreementLineItem__c>(); List<Apttus__AgreementLineItem__c> lines3 = new list<Apttus__AgreementLineItem__c>(); if(trigger.isUpdate){ if(trigger.isAfter){ AgreementTriggerClass.onAfterUpdate(trigger.newMap); } else if(trigger.isBefore){ for(Apttus__APTS_Agreement__c aptusap : trigger.New){ if(aptusap.Auto_Renewal_cust__c == 'Yes') aptusap.Apttus__Auto_Renewal__c = True; if(aptusap.Auto_Renewal_cust__c == 'No') aptusap.Apttus__Auto_Renewal__c = False; if(aptusap.Apttus__Status__c == 'Ready for Signatures' || aptusap.Apttus__Status__c == 'Activated') { agmts.add(aptusap.Id); system.debug('###############'+agmts); } if(aptusap.Apttus__Status__c == 'Activated') { agmts2.add(aptusap.Id); } } lines = [Select Id From Apttus__AgreementLineItem__c Where Apttus__AgreementId__c In: agmts AND Product_Bundle__r.Product_Bundle__c In: pbuns]; If(lines.size()>0) { lines2 =[Select Id From Apttus__AgreementLineItem__c Where Apttus__AgreementId__c In: agmts]; } lines3=[Select Id, POB__c, GL_String__c From Apttus__AgreementLineItem__c Where Apttus__AgreementId__c In:agmts2 AND Apttus__AgreementId__r.Rev_Customer__c = true]; } system.debug('$$$$$$$$$$$$$$'+lines); For(Apttus__APTS_Agreement__c apt: trigger.new) { For(Apttus__AgreementLineItem__c lis:lines2) { If(lines2.size()<2) { system.debug('***********'+lines.size()); apt.addError('Agreement needs to have more than one agreement line when there is a service pack.'); } } } //logic for SFCON-880 list<Apttus__APTS_Agreement__c> lstNewAgt = Trigger.new; list<Apttus__APTS_Agreement__c> lstOldAgt = Trigger.old; Map<Id, Apttus__APTS_Agreement__c> mapOld = new Map<Id, Apttus__APTS_Agreement__c>(); Set<Id> agtid = new Set<Id>(); For(Apttus__APTS_Agreement__c old:trigger.old) { mapOld.put(old.Id, old); } For(Apttus__APTS_Agreement__c apt: trigger.new) { //mapNew.put(apt.Id, apt); For(Apttus__AgreementLineItem__c lis:lines3) { If((lines3.size()>0) && (lis.POB__c==NULL)) { If(apt.Apttus__Status__c != mapOld.get(apt.Id).Apttus__Status__c) { system.debug('@@@@@@OLD MAP ' +mapOld.get(apt.Id).Apttus__Status__c); system.debug('@@@@@@NEW MAP ' +apt.Apttus__Status__c); agtid.add(apt.id); apt.addError('Every Agreement Line Item should have a POB populated.'); system.debug('%%%%%%%%%%%%'+lines3); } } } } //store the result for each record List<Apttus__APTS_Agreement__c> ag2 = [Select Id From Apttus__APTS_Agreement__c Where Id In: agtid]; Database.SaveResult[] srList = Database.update(ag2, false); // Iterate through each returned result and all allow partial processing of records on failure for (Database.SaveResult sr : srList) { if (sr.isSuccess()) { // Operation was successful, so get the ID of the record that was processed System.debug('Successfully inserted account. Agreement ID: ' + sr.getId()); } else { // Operation failed, so get all errors for(Database.Error err : sr.getErrors()) { System.debug('The following error has occurred.'); System.debug(err.getStatusCode() + ': ' + err.getMessage()); System.debug('Agreement fields that affected this error: ' + err.getFields()); } } } } }
In your code you are throwing an error based on conditions on line no 93
apt.addError('Every Agreement Line Item should have a POB populated.');
Thus, For(Apttus__APTS_Agreement__c apt: trigger.new) {
//mapNew.put(apt.Id, apt);
For(Apttus__AgreementLineItem__c lis:lines3) {
If((lines3.size()>0) && (lis.POB__c==NULL)) {
If(apt.Apttus__Status__c != mapOld.get(apt.Id).Apttus__Status__c && apt.ID == lis.Apttus__AgreementId__c) and now of course it works. Thanks for looking into it.