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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

After Insert is not working in Trigger

Hi, 

  Below trigger is working perfect in after update but this is not working after creating the records this is giving error below . Please suggest me what might be the issue with the code 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger quotesync.QuoteLineSyncTrigger caused an unexpected exception, contact your administrator: quotesync.QuoteLineSyncTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0QL180000008e3zGAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, autosync: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: 0Q0180000008iPYCAY (): []: (quotesync)​

 
trigger autosync on QuoteLineItem (after insert, after update) 
{
 Decimal LineMaxDiscount;
 Decimal LineMaxACV;
 Map<ID, Quote> ParentQuote = new Map<ID, Quote>();
 Integer L1Count;
 Integer L2Count;
 Integer L3Count;
 Integer L4Count;
 String SALREPID;
 String MRGID;
 String SALID;
 String CFOID;
 String Level;
   
 List<Id> listIds = new List<Id>();
 //Set<Id> listIds = new Set<Id>(); 
    
 List<Quote> QuotetList = new List<Quote>();  
 
 for (QuoteLineItem childquoteline : Trigger.new) {
        listIds.add(childquoteline.QuoteId);
    }
    
  ParentQuote = new Map<Id, Quote>([SELECT id,Level_1__c,Level_2__c,Level_3__c,Level_4__c FROM Quote WHERE ID IN :listIds]);
  
  List<AggregateResult> GetLineDiscount = [select max(Discount_Percent__c) QuoteLineMaxDiscount,SUM(UnitPrice) QuoteLineMaxACV from  QuoteLineItem where quoteid in :listIds];
     
  for (AggregateResult QuoteMaxDiscount : GetLineDiscount)
  {
  LineMaxDiscount = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxDiscount');
  LineMaxACV = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxACV');
  }
      
  Quote Qot = [select id,OpportunityID
               from quote
               where
               id in :listIds];    
      
  Opportunity Opp = [select Subscription_Term__c,ownerid
                    from opportunity
                    where
                    id = :Qot.OpportunityId];
                         
  User Usr = [select managerid from user where id = :opp.ownerid]; 
                       
   if ( Opp.Subscription_Term__c != null &&
        LineMaxACV != null &&
        LineMaxDiscount != null)
   {                       
         
     List<AggregateResult> Level1 = [select count(id) Level1Count
                                      from Discount_Schedule_Matrix__c
                                      where
                                      Subscription_Term__c = :Opp.Subscription_Term__c and
                                      ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV and
                                      Sales_Rep_Lower__c <= :LineMaxDiscount and Sales_Rep_Upper__c >= :LineMaxDiscount];
                                      
     List<AggregateResult> Level2 = [select count(id) Level2Count
                                         from Discount_Schedule_Matrix__c
                                         where
                                         Subscription_Term__c = :Opp.Subscription_Term__c and
                                         ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                         Direct_Manager_Lower__c <= :LineMaxDiscount  and Direct_Manager_Upper__c >= :LineMaxDiscount];
     
     List<AggregateResult> Level3 = [select count(id) Level3Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                        SVP_Lower__c <= :LineMaxDiscount  and SVP_Upper__c >= :LineMaxDiscount];

    List<AggregateResult> Level4 = [select count(id) Level4Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV  and
                                        CEO_CFO__c <= :LineMaxDiscount and CEO_CFO_Upper__c >= :LineMaxDiscount ];

                                    
     for (AggregateResult arLevel1 : Level1)
     {
      L1Count = (Integer)arLevel1.get('Level1Count');
     System.Debug('Level 1:' + L1Count);
     }
     
     for (AggregateResult arLevel2 : Level2)
     {
     L2Count = (Integer)arLevel2.get('Level2Count');
    System.Debug('Level 2:' + L2Count);
     }
   for (AggregateResult arLevel3 : Level3)
   {
   L3Count = (Integer)arLevel3.get('Level3Count');
   System.Debug('Level 3:' + L3Count);
   }
   for (AggregateResult arLevel4 : Level4)
   {
   L4Count = (Integer)arLevel4.get('Level4Count');
   System.Debug('Level 4:' +L4Count);
   }

   If( L1Count != 0 )
   {
    SALREPID = Opp.OwnerId;
    MRGID = null;
    SALID = null;
    CFOID = null;
    Level = '1';
   }
   else If ( L2Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = null;
    CFOID = null;
    Level = '2';
   }
   else If ( L3Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = null;
    Level = '3';
   }

   else If ( L4Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = '00580000006HV0w';
    Level = '4';
   }
              
 for (QuoteLineItem gqtl :trigger.new)
 {     
   Quote MyParentQuote = ParentQuote.get(gqtl.QuoteId);
                  
    MyParentQuote.Test_Sudhir__c = String.valueOf(LineMaxDiscount);
    MyParentQuote.Test_Sudhir_ACV__c = String.valueOf(LineMaxACV);
    MyParentQuote.Level_1__c = SALREPID;
    MyParentQuote.Level_2__c=MRGID;
    MyParentQuote.Level_3__c=SALID;
    MyParentQuote.Level_4__c=CFOID;
    MyParentQuote.Test_Sudhir_Level__c=Level;
    
    QuotetList.add(MyParentQuote);
    
     
  }  
  
  update QuotetList;
  
  
  }
 
}

Thanks
Sudhir
Best Answer chosen by sudhirn@merunetworks.com
Rajiv Penagonda 12Rajiv Penagonda 12
You have a bug in your code at line 149. The "QuotetList" is having same parent Quote records multiple times. Below is your code with the fix:
 
trigger autosync on QuoteLineItem (after insert, after update) 
{
 Decimal LineMaxDiscount;
 Decimal LineMaxACV;
 Map<ID, Quote> ParentQuote = new Map<ID, Quote>();
 Integer L1Count;
 Integer L2Count;
 Integer L3Count;
 Integer L4Count;
 String SALREPID;
 String MRGID;
 String SALID;
 String CFOID;
 String Level;
   
 List<Id> listIds = new List<Id>();
 //Set<Id> listIds = new Set<Id>(); 
    
 List<Quote> QuotetList = new List<Quote>();  
 
 for (QuoteLineItem childquoteline : Trigger.new) {
        listIds.add(childquoteline.QuoteId);
    }
    
  ParentQuote = new Map<Id, Quote>([SELECT id,Level_1__c,Level_2__c,Level_3__c,Level_4__c FROM Quote WHERE ID IN :listIds]);
  
  List<AggregateResult> GetLineDiscount = [select max(Discount_Percent__c) QuoteLineMaxDiscount,SUM(UnitPrice) QuoteLineMaxACV from  QuoteLineItem where quoteid in :listIds];
     
  for (AggregateResult QuoteMaxDiscount : GetLineDiscount)
  {
  LineMaxDiscount = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxDiscount');
  LineMaxACV = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxACV');
  }
      
  Quote Qot = [select id,OpportunityID
               from quote
               where
               id in :listIds];    
      
  Opportunity Opp = [select Subscription_Term__c,ownerid
                    from opportunity
                    where
                    id = :Qot.OpportunityId];
                         
  User Usr = [select managerid from user where id = :opp.ownerid]; 
                       
   if ( Opp.Subscription_Term__c != null &&
        LineMaxACV != null &&
        LineMaxDiscount != null)
   {                       
         
     List<AggregateResult> Level1 = [select count(id) Level1Count
                                      from Discount_Schedule_Matrix__c
                                      where
                                      Subscription_Term__c = :Opp.Subscription_Term__c and
                                      ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV and
                                      Sales_Rep_Lower__c <= :LineMaxDiscount and Sales_Rep_Upper__c >= :LineMaxDiscount];
                                      
     List<AggregateResult> Level2 = [select count(id) Level2Count
                                         from Discount_Schedule_Matrix__c
                                         where
                                         Subscription_Term__c = :Opp.Subscription_Term__c and
                                         ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                         Direct_Manager_Lower__c <= :LineMaxDiscount  and Direct_Manager_Upper__c >= :LineMaxDiscount];
     
     List<AggregateResult> Level3 = [select count(id) Level3Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                        SVP_Lower__c <= :LineMaxDiscount  and SVP_Upper__c >= :LineMaxDiscount];

    List<AggregateResult> Level4 = [select count(id) Level4Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV  and
                                        CEO_CFO__c <= :LineMaxDiscount and CEO_CFO_Upper__c >= :LineMaxDiscount ];

                                    
     for (AggregateResult arLevel1 : Level1)
     {
      L1Count = (Integer)arLevel1.get('Level1Count');
     System.Debug('Level 1:' + L1Count);
     }
     
     for (AggregateResult arLevel2 : Level2)
     {
     L2Count = (Integer)arLevel2.get('Level2Count');
    System.Debug('Level 2:' + L2Count);
     }
   for (AggregateResult arLevel3 : Level3)
   {
   L3Count = (Integer)arLevel3.get('Level3Count');
   System.Debug('Level 3:' + L3Count);
   }
   for (AggregateResult arLevel4 : Level4)
   {
   L4Count = (Integer)arLevel4.get('Level4Count');
   System.Debug('Level 4:' +L4Count);
   }

   If( L1Count != 0 )
   {
    SALREPID = Opp.OwnerId;
    MRGID = null;
    SALID = null;
    CFOID = null;
    Level = '1';
   }
   else If ( L2Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = null;
    CFOID = null;
    Level = '2';
   }
   else If ( L3Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = null;
    Level = '3';
   }

   else If ( L4Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = '00580000006HV0w';
    Level = '4';
   }
              
 for (QuoteLineItem gqtl :trigger.new)
 {     
   Quote MyParentQuote = ParentQuote.get(gqtl.QuoteId);
                  
    MyParentQuote.Test_Sudhir__c = String.valueOf(LineMaxDiscount);
    MyParentQuote.Test_Sudhir_ACV__c = String.valueOf(LineMaxACV);
    MyParentQuote.Level_1__c = SALREPID;
    MyParentQuote.Level_2__c=MRGID;
    MyParentQuote.Level_3__c=SALID;
    MyParentQuote.Level_4__c=CFOID;
    MyParentQuote.Test_Sudhir_Level__c=Level;
  }  
  
  update ParentQuote.values();
  
  
  }
 
}
if this helped, mark this as best answer.
 

All Answers

Rajiv Penagonda 12Rajiv Penagonda 12
You have a bug in your code at line 149. The "QuotetList" is having same parent Quote records multiple times. Below is your code with the fix:
 
trigger autosync on QuoteLineItem (after insert, after update) 
{
 Decimal LineMaxDiscount;
 Decimal LineMaxACV;
 Map<ID, Quote> ParentQuote = new Map<ID, Quote>();
 Integer L1Count;
 Integer L2Count;
 Integer L3Count;
 Integer L4Count;
 String SALREPID;
 String MRGID;
 String SALID;
 String CFOID;
 String Level;
   
 List<Id> listIds = new List<Id>();
 //Set<Id> listIds = new Set<Id>(); 
    
 List<Quote> QuotetList = new List<Quote>();  
 
 for (QuoteLineItem childquoteline : Trigger.new) {
        listIds.add(childquoteline.QuoteId);
    }
    
  ParentQuote = new Map<Id, Quote>([SELECT id,Level_1__c,Level_2__c,Level_3__c,Level_4__c FROM Quote WHERE ID IN :listIds]);
  
  List<AggregateResult> GetLineDiscount = [select max(Discount_Percent__c) QuoteLineMaxDiscount,SUM(UnitPrice) QuoteLineMaxACV from  QuoteLineItem where quoteid in :listIds];
     
  for (AggregateResult QuoteMaxDiscount : GetLineDiscount)
  {
  LineMaxDiscount = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxDiscount');
  LineMaxACV = (Decimal)QuoteMaxDiscount.get('QuoteLineMaxACV');
  }
      
  Quote Qot = [select id,OpportunityID
               from quote
               where
               id in :listIds];    
      
  Opportunity Opp = [select Subscription_Term__c,ownerid
                    from opportunity
                    where
                    id = :Qot.OpportunityId];
                         
  User Usr = [select managerid from user where id = :opp.ownerid]; 
                       
   if ( Opp.Subscription_Term__c != null &&
        LineMaxACV != null &&
        LineMaxDiscount != null)
   {                       
         
     List<AggregateResult> Level1 = [select count(id) Level1Count
                                      from Discount_Schedule_Matrix__c
                                      where
                                      Subscription_Term__c = :Opp.Subscription_Term__c and
                                      ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV and
                                      Sales_Rep_Lower__c <= :LineMaxDiscount and Sales_Rep_Upper__c >= :LineMaxDiscount];
                                      
     List<AggregateResult> Level2 = [select count(id) Level2Count
                                         from Discount_Schedule_Matrix__c
                                         where
                                         Subscription_Term__c = :Opp.Subscription_Term__c and
                                         ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                         Direct_Manager_Lower__c <= :LineMaxDiscount  and Direct_Manager_Upper__c >= :LineMaxDiscount];
     
     List<AggregateResult> Level3 = [select count(id) Level3Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV and ACV_Upper__c >= :LineMaxACV and
                                        SVP_Lower__c <= :LineMaxDiscount  and SVP_Upper__c >= :LineMaxDiscount];

    List<AggregateResult> Level4 = [select count(id) Level4Count
                                        from Discount_Schedule_Matrix__c
                                        where
                                        Subscription_Term__c = :Opp.Subscription_Term__c and
                                        ACV_Lower__c <= :LineMaxACV  and ACV_Upper__c >= :LineMaxACV  and
                                        CEO_CFO__c <= :LineMaxDiscount and CEO_CFO_Upper__c >= :LineMaxDiscount ];

                                    
     for (AggregateResult arLevel1 : Level1)
     {
      L1Count = (Integer)arLevel1.get('Level1Count');
     System.Debug('Level 1:' + L1Count);
     }
     
     for (AggregateResult arLevel2 : Level2)
     {
     L2Count = (Integer)arLevel2.get('Level2Count');
    System.Debug('Level 2:' + L2Count);
     }
   for (AggregateResult arLevel3 : Level3)
   {
   L3Count = (Integer)arLevel3.get('Level3Count');
   System.Debug('Level 3:' + L3Count);
   }
   for (AggregateResult arLevel4 : Level4)
   {
   L4Count = (Integer)arLevel4.get('Level4Count');
   System.Debug('Level 4:' +L4Count);
   }

   If( L1Count != 0 )
   {
    SALREPID = Opp.OwnerId;
    MRGID = null;
    SALID = null;
    CFOID = null;
    Level = '1';
   }
   else If ( L2Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = null;
    CFOID = null;
    Level = '2';
   }
   else If ( L3Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = null;
    Level = '3';
   }

   else If ( L4Count != 0 )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = '00580000006HV0w';
    Level = '4';
   }
              
 for (QuoteLineItem gqtl :trigger.new)
 {     
   Quote MyParentQuote = ParentQuote.get(gqtl.QuoteId);
                  
    MyParentQuote.Test_Sudhir__c = String.valueOf(LineMaxDiscount);
    MyParentQuote.Test_Sudhir_ACV__c = String.valueOf(LineMaxACV);
    MyParentQuote.Level_1__c = SALREPID;
    MyParentQuote.Level_2__c=MRGID;
    MyParentQuote.Level_3__c=SALID;
    MyParentQuote.Level_4__c=CFOID;
    MyParentQuote.Test_Sudhir_Level__c=Level;
  }  
  
  update ParentQuote.values();
  
  
  }
 
}
if this helped, mark this as best answer.
 
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com
Wonderfull Rajiv you fixed the issue.