function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ishchopraishchopra 

Apex Mass Approval conflicting with Class

Hello Experts,

Good Morning!

I have a scenario which is taking lot of time to resolve one problem.

What i am trying to Achieve?

When doing mass approval on opportunities we get this error message - THERE WERE CUSTOM VALIDATION ERROR(S) ENCOUNTERED WHILE SAVING THE AFFECTED RECORD. THE FIRST VALIDATION ERROR ENCOUNTERED WAS "APEX TRIGGER AFTERTRIGGER_OPPORTUNITY CAUSED AN UNEXPECTED EXCEPTION, VALIDATION ERROR WHILE SAVING RECORD(S)
SYSTEM.LISTeXCEPTION: DUPLICATE ID IN LIST : 


Apex Class
 
public class DiscountedProductsApproved {
    public static void SetProductsApproved(Opportunity[] theNewTrigger,Opportunity[] theOldTrigger){
        
        //Recast the inbound data to its original SObject type
        List<Opportunity> theNewOpportunity = (List<Opportunity>) theNewTrigger;
        List<Opportunity> theOldOpportunity = (List<Opportunity>) theOldTrigger;
        
        //Find the Approved Opps with Line Items that need Approving
        List<Opportunity> theNewOpportunitywithLineItems = [select id, name, (select id,Discount_Approved__c,Discount_Amount__c from OpportunityLineItems WHERE Discount_Approved__c = False AND Discount_Amount__c >0)from Opportunity where Id IN :theNewOpportunity];
        
        //List to hold the Line Items to be updated
        list<OpportunityLineItem> oLineItemsToUpdate = new list<OpportunityLineItem>{};
        Integer j=0;    
        //Loop through the Opps from the Trigger        
        for (Integer i = 0; i < theNewOpportunity.size(); i++) {
        
        //Only process the Opps that have had the Discount_Approval_Status__c field updated
            If(theNewOpportunity[i].Discount_Approval_Status__c == 'Approved'
               && theOldOpportunity[i].Discount_Approval_Status__c != 'Approved'){
        
        //Loop through the Opps and Line Items that need Approving            
                   for(Opportunity o: theNewOpportunitywithLineItems){
                       for(OpportunityLineItem oli: o.OpportunityLineItems){
                           oli.Discount_Approved__c = True;
                           oLineItemsToUpdate.add(oli);
                           j++;
                       }
                   }
               }
        }
        //Debug values
        system.debug('The number of Opportunity Line Items processed (j): ' + j);
        system.debug('The number of Opps processed (size): ' + theNewOpportunity.size());
        system.debug('The number of Opps with line items processed (size): ' + theNewOpportunitywithLineItems.size());
        
        update oLineItemsToUpdate;
    }

}

Apex Trigger AfterTrigger_Opportunity
 
trigger AfterTrigger_Opportunity on Opportunity (after update, after insert, after delete) {
    
     if(Trigger.isAfter){
        if(Trigger.isUpdate){
            //Send the Opportunity data to the Apex class for Rollup
            EventStakeholderRollup.rollupStakeholder(Trigger.new);
            //Send Opportunity data to the Apex class for Approval Submission
            SubmitOppForApproval.SubmitForApproval(Trigger.new,Trigger.old);
            //Send Opportunity data to the Apex class to update Approved Line Items
            for(Opportunity opp: Trigger.new){
                if(opp.No_Products_Requiring_Discount_Approval__c>0){
                    DiscountedProductsApproved.SetProductsApproved(Trigger.new,Trigger.old);
                }
            }
        } 
        if(Trigger.isInsert){
            //Send the Opportunity data to the Apex class
            EventStakeholderRollup.rollupStakeholder(Trigger.new);
        } 
        if(Trigger.isDelete){
            //Send the Opportunity data to the Apex class
            EventStakeholderRollup.rollupStakeholder(Trigger.old);
        } 
     }
}

My Investigation

The error is related to opportunity line item as it says in the error message, somehow the opportunity line item is being repeated in the list however, this only happens when product name is same on the opportunity for example: If i am taking two opportunities for mass approval and both opportunity has same products under opportunity line item then it will give the above error message but if opportunity has different products then process go through without any problems.

Can somebody help pease? we have 150 approvals daily and list is getting bigger.

thanks
Best Answer chosen by ishchopra
ishchopraishchopra
Hello there,

The case is resolved - i have de-duped the list by creating a set and then putting back to another list.

thanks for looking