+ Start a Discussion
Sushma R IyerSushma R Iyer 

Apex trigger trg_QuoteLineItem has caused an unexpected exception. Contact your administrator: trg_QuoteLineItem: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 0QL1E000004rJkSWAU: ()

Hi All,

When a user is trying to create a Quote for an Opportunity then the below Error is displayed :

Apex trigger trg_QuoteLineItem has caused an unexpected exception. Contact your administrator: trg_QuoteLineItem: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 0QL1E000004rJkSWAU: ()

Also since this code is not developed by me , I am unable to understand on whats causing the issue in the below Code :
 
trigger trg_QuoteLineItem on QuoteLineItem ( after insert, after update, after delete, before insert, before update ) {
    
    cls_trg_QuoteLineItem handler = new cls_trg_QuoteLineItem();
    
    if (trigger.isafter && trigger.isInsert) {
        system.debug('inside after insert');
      handler.removeWarrantyFromQuote( trigger.new );
    }
    
    if ( trigger.isAfter ) {
        
        if ( trigger.isInsert || trigger.isUpdate ) {
                    
            //Set Quote's 'QLI Rebate Names' and 'QLI_Conga_Template_ID_s_c' Process
            handler.updateQuoteFromQLIInsert(trigger.new);
            handler.updatePromotionListPrice(trigger.new);
        }
        
        if ( trigger.isDelete ){
            
            //Set Quote's 'QLI Rebate Names' and 'QLI_Conga_Template_ID_s_c' Process
            handler.updateQuoteFromQLIInsert(trigger.old);
        }
    } else if(trigger.Isbefore) {
        
        //handler.updatePromotionListPrice(trigger.new);
    }
}

Please Help...Thanks for All Help in Advance
GrumpyDevGrumpyDev
Hi Sushma,

Probably somewhere in the "cls_trg_QuoteLineItem" Class(The handler class, you provided the trigger class)  The same Id/Record is entered Into a List that been Updated twice - Thats the Error...

Usally this can be solved by using Set or Map instead of List

In order to help you a bit more please add the handler class.

Roi.
Raj VakatiRaj Vakati
Can you give me this class also cls_trg_QuoteLineItem  ? i wil look into it 
Sushma R IyerSushma R Iyer
Hi All,
PFB the requested :
public with sharing class cls_trg_QuoteLineItem {

    public void updatePromotionListPrice ( List<QuoteLineItem> newQuoteList ) {
        
        Set<Id> QuoteIdSet = new Set<Id>();
        Map<Id,Id> quoteIdAndPromotionIdMap = new Map<Id,Id>();
        Map<Id,List<Promotion_Product__c>> PromotionIdAndPromProductMap = new Map<Id,List<Promotion_Product__c>>();
        Set<Id> promotionIdSet = new Set<Id>();
        List<QuoteLineItem> updateQuoteLineItemList = new List<QuoteLineItem>();
        
        if( newQuoteList != NULL && newQuoteList.Size() > 0 ) {
            
            for( QuoteLineItem qLI : newQuoteList ) {
                
                if(!cls_GlobalConstants.qliIdSet.contains(qLI.Id)){
                
                    QuoteIdSet.add(qLI.QuoteId);
                    cls_GlobalConstants.qliIdSet.add(qLI.Id);
                }
            }
            
            if( QuoteIdSet != NULL && QuoteIdSet.Size() > 0 ) {
                
                for(Quote qt : [SELECT Id, Name, Promotion_Name__c FROM Quote WHERE Id IN : QuoteIdSet And Promotion_Name__c != NULL]) {
                    
                    quoteIdAndPromotionIdMap.put(qt.Id, qt.Promotion_Name__c);
                    promotionIdSet.add(qt.Promotion_Name__c);                    
                }
                
                System.Debug('<----quoteIdAndPromotionIdMap---->'+quoteIdAndPromotionIdMap);

                if( quoteIdAndPromotionIdMap != NULL && quoteIdAndPromotionIdMap.Size() > 0 && promotionIdSet != null && promotionIdSet.size() > 0) {
                    
                    for(Promotion_Product__c promProd : [SELECT Id, Name, Oracle_Modifier_ID__c, Product_Name__c, Promotional_Price__c, Promotion_Name__c FROM Promotion_Product__c WHERE Promotion_Name__c In : promotionIdSet ]){
                        
                        if( PromotionIdAndPromProductMap.containsKey(promProd.Promotion_Name__c) ) {
                            
                            PromotionIdAndPromProductMap.get(promProd.Promotion_Name__c).add(promProd);
                        } else {
                            
                            PromotionIdAndPromProductMap.put(promProd.Promotion_Name__c,new List<Promotion_Product__c>{promProd});
                        }
                        
                    }
                    
                    System.Debug('<----PromotionIdAndPromProductMap---->'+PromotionIdAndPromProductMap);
                    
                    if(PromotionIdAndPromProductMap != null && PromotionIdAndPromProductMap.size() > 0) {
                        
                        for( QuoteLineItem qLI : [SELECT Id, Modifier_ID__c, ListPrice, Promotion_List_Price__c, QuoteId, Product2Id FROM QuoteLineItem WHERE Id IN : newQuoteList ] ) {
                            
                            if( quoteIdAndPromotionIdMap.containsKey(qLI.QuoteId) && quoteIdAndPromotionIdMap.get(qLI.QuoteId) != null && PromotionIdAndPromProductMap.containsKey(quoteIdAndPromotionIdMap.get(qLI.QuoteId)) && PromotionIdAndPromProductMap.get(quoteIdAndPromotionIdMap.get(qLI.QuoteId)) != null) {
                            
                                for(Promotion_Product__c pro : PromotionIdAndPromProductMap.get(quoteIdAndPromotionIdMap.get(qLI.QuoteId))) {
                                
                                    System.Debug('--pro.Product_Name__c--'+pro.Product_Name__c);  
                                    System.Debug('--qLI.Product2Id--'+qLI.Product2Id);  
                                    System.Debug('--pro.Promotional_Price__c--'+pro.Promotional_Price__c);  
                                    System.Debug('--qLI.ListPrice--'+qLI.ListPrice); 
                                    System.Debug('--qLI--'+[SELECT Id, Conga_Template_ID__c, QuoteId, ListPrice, Product2Id, Product2.System__c, Product2.Part_Type__c, Product2.Marketing_Rebate__c, Quote.Promotion_Name__c FROM QuoteLineItem WHERE Id = :qLI.Id]);
                                    
                                    if(pro.Product_Name__c == qLI.Product2Id) {
                                        
                                        qLI.Promotion_List_Price__c = (qLI.ListPrice != NULL && qLI.ListPrice >= 0) ? qLI.ListPrice + pro.Promotional_Price__c : pro.Promotional_Price__c;
                                        qLI.Modifier_ID__c = pro.Oracle_Modifier_ID__c;
                                        
                                        updateQuoteLineItemList.add(qLI);
                                    }
                                }
                            }
                        }
                        
                        if(updateQuoteLineItemList != NULL && updateQuoteLineItemList.size() > 0){
                        
                            update updateQuoteLineItemList;
                        }
                    }
                }
            }
        }
    }

    public void updateQuoteFromQLIInsert( List<QuoteLineItem> newQLI_List ) {   
        
        Map<Id, List<QuoteLineItem>> quoteIdAndQLIList_Map = new Map<Id, List<QuoteLineItem>>();
        Map<Id, List<QuoteLineItem>> quoteIdAndQLIListForCongaTemp_Map = new Map<Id, List<QuoteLineItem>>();
        Set<Id> quoteId_Set = new Set<Id>();
        List<Quote> updateQuoteList = new List<Quote>();
        
        if ( newQLI_List != NULL && newQLI_List.size() > 0 ){
        
            //Retrive QuoteId 
            for ( QuoteLineItem qli : newQLI_List ){
                quoteId_Set.add(qli.QuoteId);
            }
            System.debug('----quoteId_Set--->>>'+quoteId_Set);
            
            //Get All related QLI records
            if ( quoteId_Set != NULL && quoteId_Set.size() > 0 ) {
            
                for ( QuoteLineItem qli : [SELECT Id, Conga_Template_ID__c, QuoteId, ListPrice, Product2Id, 
                                           Product2.System__c, Product2.Part_Type__c, Product2.Marketing_Rebate__c, Quote.Promotion_Name__c
                                           FROM QuoteLineItem 
                                           WHERE QuoteId IN: quoteId_Set
                                           AND ( Product2.Marketing_Rebate__c = 'Yes' OR Product2.Part_Type__c = 'Systems')]){                    
                    System.debug(':::Promotion Name:::'+Quote.Promotion_Name__c);
                    //Quote Id and related QLI Map
                    if ( qli.QuoteId != NULL ){
                        if ( quoteIdAndQLIList_Map.containsKey( qli.QuoteId ) ){
                            List<QuoteLineItem> existQLI = new List<QuoteLineItem>();
                            existQLI = quoteIdAndQLIList_Map.get( qli.QuoteId );
                            existQLI.add(qli);
                            quoteIdAndQLIList_Map.put( qli.QuoteId , existQLI) ;
                        }else {
                            List<QuoteLineItem> newQLI = new List<QuoteLineItem>();                        
                            newQLI.add(qli);
                            quoteIdAndQLIList_Map.put( qli.QuoteId , newQLI) ;
                        }
                    }                                    
                }
                System.debug('::::quoteIdAndQLIList_Map:::::'+quoteIdAndQLIList_Map); 
                
                //Set Quote field mapping
                for ( Id qteId : quoteId_Set ){
                
                    Quote quoteRec = new Quote(Id=qteId);
                    quoteRec.QLI_Rebate_Names__c = '';
                    quoteRec.QLI_Conga_Template_ID_s__c = '';
                    
                    String ProductStr ;
                    String congaStr;
                    
                    if ( quoteIdAndQLIList_Map != NULL && quoteIdAndQLIList_Map.size() > 0 && 
                         quoteIdAndQLIList_Map.containsKey(qteId) ) {
                        
                        for ( QuoteLineItem qli : quoteIdAndQLIList_Map.get(qteId) ) {
                        
                            //Set Quote's QLI Rebate Names
                            if ( qli.Product2.Marketing_Rebate__c == 'Yes' ) {                                
                                if ( !String.isBlank(qli.Product2.System__c) ) {                                
                                    if ( !String.isBlank(ProductStr) ){ 
                                        if(!ProductStr.contains(qli.Product2.System__c)) {
                                            System.debug('::::ProductStr:::::' + ProductStr); 
                                            ProductStr = ProductStr +', '+qli.Product2.System__c;  
                                        }
                                    }else {                                    
                                        ProductStr = qli.Product2.System__c;                                        
                                    }
                                }   
                            }
                            
                            //Set Quote's QLI_Conga_Template_ID_s_c
                            if ( qli.Product2.Part_Type__c == 'Systems' ) {                                
                                if ( !String.isBlank(qli.Conga_Template_ID__c) ) {   
                                    
                                    String tempId = qli.Conga_Template_ID__c;
                                    
                                    //if ( tempId.length() > 15 ) {                                        
                                        //tempId = tempId.substring(0,15);
                                    //}
                                                                 
                                    if ( !String.isBlank(congaStr) ){
                                        congaStr = congaStr +','+tempId ;                                        
                                    }else {                                                                          
                                        congaStr= tempId ;
                                    }
                                }   
                            }
                        }
                        System.debug('::ProductStr Final:::'+ProductStr);
                    }
                    
                    if ( !String.isBlank(ProductStr) ) {
                    
                        if ( ProductStr.length() > 255 ){
                            ProductStr = ProductStr.substring(0,255);
                            System.debug(':::ProductStr:::'+ProductStr);
                        }  
                    }
                    
                    if ( !String.isBlank(congaStr) ) {
                    
                        if ( congaStr.length() > 255 ){
                            congaStr = congaStr.substring(0,255);
                            System.debug(':::congaStr:::'+congaStr);
                        }
                    }
                    
                    quoteRec.QLI_Rebate_Names__c = ProductStr;
                    quoteRec.QLI_Conga_Template_ID_s__c = congaStr;
                    System.debug('::quoteRec:::'+quoteRec);
                    updateQuoteList.add(quoteRec);
                }
            }                
                       
            
            if ( updateQuoteList != NULL && updateQuoteList.size() > 0 ){
                update updateQuoteList;
                System.debug(':::::updateQuoteList::::'+updateQuoteList);
            } 
        }        
    }
    
    /* SYNR-171: remove warranty product from Quate
  *
  * @author  : Shira Hai
  * @date    : 13/12/17
  *
  * @method   removeWarrantyFromQuote
  * @param    Map<Id, OpportunityLineItem>  : oldMap
  */
    public void removeWarrantyFromQuote( List<QuoteLineItem> newQuoteList ){
        
        List<QuoteLineItem> noWarrList = new List<QuoteLineItem>();
        Map<Id,QuoteLineItem> quoteliToDelete = new Map<Id,QuoteLineItem>();
        Set <Id> quoteIdSet = new Set <Id>();
        for (QuoteLineItem qualiItem: newQuoteList){
            quoteIdSet.add(qualiItem.QuoteId);
        }
         
        Map <Id, Quote> QuoteNoWarMap = new Map<Id,Quote> ([SELECT Id, warranty__c FROM Quote WHERE warranty__c =:'No' AND Id in: quoteIdSet]);
        
        for(QuoteLineItem qualiItem: newQuoteList){
            if(QuoteNoWarMap.containsKey(qualiItem.QuoteId)){
                noWarrList.add(qualiItem);
            }
        }
     
        
       List <Warranty_Product2__mdt> warData = [SELECT Default_warranty_Item__c, Model__c,RegionPickList__c FROM Warranty_Product2__mdt];
  
        if(!warData.isEmpty() && !noWarrList.isEmpty() ){
            for(QuoteLineItem quoliItem : noWarrList){
                for(Warranty_Product2__mdt warProItem: warData){
                    if(quoliItem.Product_Code_Warrenty__c == warProItem.Default_warranty_Item__c){
                        QuoteLineItem quetnewItem = new QuoteLineItem();
                        quetnewItem.Id = quoliItem.id;
                        quoteliToDelete.put(quetnewItem.Id,quetnewItem);
                    }
                }
            }
      }
        if(!quoteliToDelete.isEmpty()){
            delete quoteliToDelete.Values();
        }
    }
}

 
GrumpyDevGrumpyDev
Hi Sushma,

The Error happens becuase on Line : 
067 -     updateQuoteLineItemList.add(qLI);
You are entering to the update list the same qLI more than one time.
It also probably means you are calculating wrong this :

qLI.Promotion_List_Price__c = (qLI.ListPrice != NULL && qLI.ListPrice >= 0) ? qLI.ListPrice + pro.Promotional_Price__c :
pro.Promotional_Price__c;

 qLI.Modifier_ID__c = pro.Oracle_Modifier_ID__c;

First you should remove the updateQuoteLineItemList.add(qLI); outside of the inner loop.
seconed you should check whats the buisness requirment if more than one Promotion_Product__c fits the conditions i can only guess that
qLI.Promotion_List_Price__c should be += instead of =.

Good Luck.


 
Sushma R IyerSushma R Iyer
Hi Roi,

Thanks a lot! Will  check and let you know.