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 

Trigger is working on After Update but not after Insert

Hi,

   I wrote a trigger on quotelines to update fields in quote below is the trigger when am creating quotelines for the first time trigger is not working as expected but after than its working please suggest me what is the mistake in the logic or code which i have written please let me know. 
 
trigger GenQuoteApproval on QuoteLineItem (After Insert, After Update) {
  
  Decimal LineMaxDiscount;
  Decimal LineMaxACV;
  Decimal SubLineMaxDiscount;
  Decimal SubLineMaxACV;
  Decimal SerLineMaxDiscount;
  Decimal SerLineMaxACV;
  Map<ID, Quote> ParentQuote = new Map<ID, Quote>();
  String SALREPID;
  String MRGID;
  String SALID;
  String CFOID;
  String Level;
  Integer GSublevel1Count;
  Integer GSublevel2Count;
  Integer GSublevel3Count;
  Integer GSublevel4Count;
  Integer GSerlevel1Count;
  Integer GSerlevel2Count;
  Integer GSerlevel3Count;

 List<Id> listIds = new List<Id>();
 //Set<Id> listIds = new Set<Id>();

 List<Quote> QuotetList = new List<Quote>();

 /* Get Quote ID */
 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]);

  /* Get service list of all quote id */
  list<id> serviceid = new list<id>();
  for(QuoteLineItem getserviceid : [select id  from  QuoteLineItem
                                     where quoteid in :listIds and
                                           product2.Productcode  like 'CBSVC%'] )
   {
      serviceid.add(getserviceid.id);

   }

  /* Get subscription list of all quote id */
  list<id> subscriptionid = new list<id>();
  for(QuoteLineItem getsubscriptionid : [select id  from  QuoteLineItem
                                     where quoteid in :listIds and
                                           Subscription_Terms__c <> 0] )
   {
      subscriptionid.add(getsubscriptionid.id);

   }

 /* Subscription Discount and ACV */
    List<AggregateResult> MaxSubscription = [select max(Discount_Percent__c) SubQuoteLineMaxDiscount,sum(f_ACV__c) SubQuoteLineMaxACV
                                                   from  QuoteLineItem
                                                   where quoteid in :listIds and Subscription_Terms__c <> 0 and
                                                         ID not in :serviceid
                                                 ];

  for (AggregateResult SubQuoteMaxDiscount : MaxSubscription)
  {
  SubLineMaxDiscount = (Decimal)SubQuoteMaxDiscount.get('SubQuoteLineMaxDiscount');
  SubLineMaxACV = (Decimal)SubQuoteMaxDiscount.get('SubQuoteLineMaxACV');
  }

   system.debug('Subscription Line Discount  :' + SubLineMaxDiscount);
   system.debug('Subscription Line ACV  :' + SubLineMaxACV);

   /* Service Discount and ACV */
  List<AggregateResult> MaxService = [select max(Discount_Percent__c) SerQuoteLineMaxDiscount,SUM(UnitPrice) SerQuoteLineMaxACV
                                      from  QuoteLineItem
                                      where quoteid in :listIds and product2.Productcode  like 'CBSVC%' and
                                            id not in :subscriptionid];

  for (AggregateResult SerQuoteMaxDiscount : MaxService)
  {
  SerLineMaxDiscount = (Decimal)SerQuoteMaxDiscount.get('SerQuoteLineMaxDiscount');
  SerLineMaxACV = (Decimal)SerQuoteMaxDiscount.get('SerQuoteLineMaxACV');
  }

   system.debug('Service Line Discount  :' + SerLineMaxDiscount);
   system.debug('Service Line ACV  :' + SerLineMaxACV);

  Quote Qot = [select id,OpportunityID
               from quote
               where
               id in :listIds];

  Opportunity Opp = [select Subscription_Term__c,ownerid
                    from opportunity
                    where
                    id = :Qot.OpportunityId];

  system.debug('Subscription Term  :' + Opp.Subscription_Term__c);

   User Usr = [select managerid from user where id = :opp.ownerid];

 /* Subscription Query to get level 1..4 values */
   if ( Opp.Subscription_Term__c != null &&
        SubLineMaxACV != null &&
        SubLineMaxDiscount != null)
   {
       GSublevel1Count = ApprovalUtils.SubLevel1(Opp.Subscription_Term__c , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level1 :' + GSublevel1Count);

       GSublevel2Count = ApprovalUtils.SubLevel2(Opp.Subscription_Term__c , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level2 :' + GSublevel2Count);

       GSublevel3Count = ApprovalUtils.SubLevel3(Opp.Subscription_Term__c , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level3 :' + GSublevel3Count);

       GSublevel4Count = ApprovalUtils.SubLevel4(Opp.Subscription_Term__c , SubLineMaxACV , SubLineMaxDiscount);

       system.debug('Subscription Level4 :' + GSublevel4Count);

    If( GSublevel1Count >= 1  )
     {
      SALREPID = Opp.OwnerId;
      Level = '1sub';
      }   
     
    If (GSublevel2Count >= 1 )
    {
      SALREPID = NULL;
      MRGID = Usr.managerid;
      Level = '2sub';
    }
       
    If ( GSublevel3Count >= 1)
    {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    Level = '3sub';
    }
       
    If ( GSublevel4Count >= 1 )
    {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    CFOID = '00580000006HV0w';
    Level = '4sub';
    }       
   }

    /* Service Query to get level 1..4 values */
   if (SerLineMaxACV != null && SerLineMaxDiscount != null)
    {
       GSerlevel1Count = ApprovalUtils.SerLevel1(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level1 :' + GSerlevel1Count);

        GSerlevel2Count = ApprovalUtils.SerLevel2(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level2 :' + GSerlevel2Count);

        GSerlevel3Count = ApprovalUtils.SerLevel3(SerLineMaxACV,SerLineMaxDiscount);

       system.debug('Service Level3 :' + GSerlevel3Count);
        
   If(   GSerlevel1Count >= 1 )
   {
    SALREPID = Opp.OwnerId;
     Level = '1ser';
     system.debug('GSerlevel1Count :' + GSerlevel1Count);
   } 

   else If (GSerlevel2Count >= 1  )
   {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    Level = '2ser';
   }
  
   else If ( GSerlevel3Count >= 1 )
  {
    SALREPID = NULL;
    MRGID = Usr.managerid;
    SALID = '00580000007jaoA';
    Level = '3ser';
   }    
  }

  
    //system.debug('Which Level :' + Level);
    system.debug('Sales Rep :' + SALREPID);
    system.debug('Manager :' + MRGID);
    system.debug('Sales Ops :' + SALID);
    system.debug('CEO CFO :'  + CFOID);

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();


}

Thanks
Sudhir
doravmondoravmon
I will suggest you add a system.debug around line 199...to see if there is any new trigger or not..
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for your replace what should be the system.debug content to add in line 199 just a message to print? 

Thanks
Sudhir
doravmondoravmon
doesn't matter, just want to make sure it did come in to the for loop... you can add more debugs to check..
sudhirn@merunetworks.comsudhirn@merunetworks.com
I added a system.debug message inside loop message is getting print i am able to see in debug log. Please suggest me what might be the issue how to change the logic here. 
for (QuoteLineItem gqtl : Trigger.new)
 {
   system.debug('sudhir is this working');
   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;
     system.debug('sudhir is this working last time');
  }


Thanks
Sudhir
doravmondoravmon
ah....I might know why....is this trigger only work for you when 'update'? not 'insert'?   you don't have any 'insert' code ...
sudhirn@merunetworks.comsudhirn@merunetworks.com
How should I be modifying the code here ? any suggestion? 
doravmondoravmon
after the for loop, try this:
try
{update ParentQuote.values();}
catch(Exception ex)
{system.debug(ex.getMessage());
}

See what's the exception is.