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
shakila Gshakila G 

I have Trigger in quote and Quote line time

Hi All,
I have two triggers in quote and Quote line item
Quote Trigger; v2_QuoteTaxUpdate
Quote line Item Trigger: RolluplineitemTrigger

while testing the RolluplineitemTrigger in QuotelineItem am  getting the following error

User-added image

kindly Let me know where am doing mistake
RolluplineitemTrigger;

trigger RolluplineitemTrigger on QuoteLineItem  (after insert, after update) {

    List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}

}

v2_QuoteTaxUpdate:

trigger v2_QuoteTaxUpdate on Quote (after insert, after update) {

    list<QuoteLineItem> lstUpdateQLI = new list<QuoteLineItem>();
    set<id> setQuoteId = new set<id>();
     set<id> setQuoteIdTaxUpd = new set<id>();
    set<id> setQLIId = new set<id>();
    set<String> setHSNCode = new set<String>();  
    List<Id> VQuoteID = new List<Id>();  
    map<string, list<Tax_Master__c>>mapTaxMasToTaxKey = new map<string, list<Tax_Master__c>>();    
        
    for(Quote iQLI : Trigger.New){   
        If(Trigger.Isinsert) {
        
            if(iQLI.Tax_Classification__c !=''){
            
                setQuoteId.add(iQLI.id);
                setQuoteIdTaxUpd.add(iQLI.id);
                system.debug('****setQLIId'+setQLIId);       
                
            }
        }
        
        if(Trigger.isupdate) {
        
         if(iQLI.Tax_Classification__c!=''){
            
                setQuoteId.add(iQLI.id);
                setQuoteIdTaxUpd.add(iQLI.id);
                system.debug('****setQLIId'+setQLIId);       
          }            
          if(iQLI.Discount_Amount__c!=Null){  
          
              setQuoteId.add(iQLI.id);
              system.debug('****setQLIId'+setQLIId);  
          }
        
        }
        
    }
    
        
    
    if(setQuoteId.size() > 0){
    
        for(Quote iQuot : [SELECT id,(SELECT id, Product2.HSN_Code__c FROM QuoteLineItems ) 
            FROM Quote WHERE Id IN :setQuoteIdTaxUpd]){
        
            for(QuoteLineItem iQLI : iQuot.QuoteLineItems){
                  
                    if(iQLI.Product2.HSN_Code__c != ''){
                    
                        setHSNCode.add(iQLI.Product2.HSN_Code__c);
                    }    
            }   
            system.debug('****setHSNCode'+setHSNCode);                    
        }
        
        if(setHSNCode.size() > 0){
        
            if(setHSNCode.size() > 0){
            
                for(Tax_Master__c iTM : [SELECT id, Product_Family__c,Product_Sub_Family__c,HSN_Code__c, IGST__c, CGST__c, SGST__c, Active__c,Start_Date__c,End_date__c FROM Tax_Master__c  WHERE HSN_Code__c IN :setHSNCode ]){
                
                     String ikey = iTM.HSN_Code__c;
                    if(!String.isblank(iTM.Product_Family__c))
                    {
                        ikey = ikey + iTM.Product_Family__c;                         
                    } 
                    if(!String.isblank(iTM.Product_Sub_Family__c))
                    {
                        ikey = ikey + iTM.Product_Sub_Family__c;                         
                    } 
                    list<Tax_Master__c> lstTax = new list<Tax_Master__c>();
                    lstTax.add(iTM);
                    if(mapTaxMasToTaxKey.containskey(ikey))
                    {
                        lstTax.addall(mapTaxMasToTaxKey.get(ikey));
                    }
                    mapTaxMasToTaxKey.put(ikey, lstTax); 
                                 
                 }   
                    system.debug('****mapTaxMasToTaxKey'+mapTaxMasToTaxKey);                    
 
            }  
            
                  
            
            for(Quote iQuot : [SELECT id, Tax_Classification__c, Discount_Amount__c,Anki_total_Amt__c,
                (SELECT CreatedDate,Product2.family, Product2.Sub_Family__c,Product2.HSN_Code__c, Tax_Master__c, IGST__c, CGST__c, SGST__c FROM QuoteLineItems) 
                FROM Quote WHERE Id IN :setQuoteId]){
            
                for(QuoteLineItem iQLI : iQuot.QuoteLineItems){  
                       
                       system.debug('****iQuot.Discount_Amount__c'+iQuot.Discount_Amount__c); 
                       system.debug('****iQuot.Anki_total_Amt__c'+iQuot.Anki_total_Amt__c); 
                          
                          if(iQuot.Discount_Amount__c <= 0 ){
                                iQLI.Q_Discount__c = 0;    
                          
                            } else {
                                if(iQuot.Discount_Amount__c != null && iQuot.Anki_total_Amt__c !=null){
                                iQLI.Q_Discount__c =(iQuot.Discount_Amount__c/iQuot.Anki_total_Amt__c) * 100;
                                
                                }
                            } 
                                                       
                         system.debug('****Q_Discount__c'+iQLI.Q_Discount__c);
                        Date myDate =  date.parse('11/10/2017');
                        Date myDate2 = date.parse(iqli.CreatedDate.day()+'/'+iqli.CreatedDate.month()+'/'+iqli.CreatedDate.year());
                        
                        Tax_Master__c itax = new Tax_Master__c();
                        
                        
                        if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family+iQLI.Product2.Sub_Family__c) && myDate2 >= myDate ){
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family+iQLI.Product2.Sub_Family__c))
                            {
                                if(irow.Start_Date__c <= iqli.CreatedDate && irow.End_Date__c  >= iqli.CreatedDate)
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }
                        else if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family) && myDate2 >= myDate ){
                            
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c+iQLI.Product2.Family))
                            {
                                if(irow.Start_Date__c <= iqli.CreatedDate && irow.End_Date__c  >= iqli.CreatedDate)
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }else if(mapTaxMasToTaxKey.containsKey(iQLI.Product2.HSN_Code__c))
                        {
                            for(Tax_Master__c irow : mapTaxMasToTaxKey.get(iQLI.Product2.HSN_Code__c))
                            {
                                if(irow.Start_Date__c <= iqli.CreatedDate && irow.End_Date__c  >= iqli.CreatedDate)
                                {
                                
                                    itax = irow;
                                    break;
                                }
                            }
                        }
                        
                        if(!String.isblank(itax.id)){
                    
                        iQLI.Tax_Master__c = itax.id;
                        
                        
                        
                        if(iQuot.Tax_Classification__c == 'IGST'){
                        
                            iQLI.IGST__c = itax.IGST__c;
                            iQLI.SGST__c = 0; 
                            iQLI.CGST__c = 0;
                            //iQLI.Q_Discount__c = iQuot.Ankidyne_Discount_Percentage__c;
                        } 
                        
                        if(iQuot.Tax_Classification__c == NUll){
                        
                            iQLI.SGST__c = 0;
                            iQLI.CGST__c = 0;    
                            iQLI.IGST__c = 0;  
                            //iQLI.Q_Discount__c = iQuot.Ankidyne_Discount_Percentage__c;                      
                        }
                        if(iQuot.Tax_Classification__c == 'SGST'){
                        
                            iQLI.SGST__c = itax.SGST__c;
                            iQLI.CGST__c = itax.CGST__c;    
                            iQLI.IGST__c = 0;  
                            //iQLI.Q_Discount__c = iQuot.Ankidyne_Discount_Percentage__c;                      
                        }                       
                        
                        lstUpdateQLI.add(iQLI);                            
                    }    
                                         
                }    
            }  
            
            if(lstUpdateQLI.size() > 0){            
                update lstUpdateQLI;
            }          
        }
    }
}

 
Best Answer chosen by shakila G
Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi
Try this , if it works mark as
public class CheckRecurisive{
public static boolean check = true;
public static boolean runOnce(){
  if(check){
check = !check;
return true;
}
else{
return flase;
}
}
}

// your trigger updated
trigger RolluplineitemTrigger on QuoteLineItem  (after insert, after update) {
if(CheckRecurisive.runOnce()){
 List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}
}
​}

All Answers

Steven NsubugaSteven Nsubuga
You're caught in an infinite cyclic loop! Each of your 2 triggers causes the other to run. You need to set it u that each trigger only runs once.
Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi

Please update your trigger like this. this is coming because of recursiveness. handle by using static keyword.

 

I updated the Trigger , please find below. 

 

If you found this is useful. mark this as a Best Solution

public class CheckRecurisive{
public static boolean check = true;
public boolean runOnce(){
  if(check){
check = !check;
return true;
}
else{
return flase;
}
}
}

// your trigger updated
trigger RolluplineitemTrigger on QuoteLineItem  (after insert, after update) {
if(CheckRecurisive.runOnce()){
 List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}
}
​}


 
VamsiVamsi
Hi,

Add a check in the RolluplineitemTrigger on QuoteLine item looks like recursive. 
for(QuoteLineItem p: Trigger.new) 
{
       if(Trigger.isAfter && Trigger.IsUpdate)
       {
             if(p.Stock_Not_Available__c!= Trigger.oldMap.get(p.id).Stock_Not_Available__c)
              {
                VQuoteID.add(p.QuoteiD);

              }
       }else  
       { 
         VQuoteID.add(p.QuoteiD);
       }
 }


Hope this helps..!!


Please mark as best answer if the above helps ...!!!

shakila Gshakila G
Hi Deepak, Thanks for the reply. Am getting following error. Compile Error: Non static method cannot be referenced from a static context: Boolean CheckRecurisive.runOnce() at line 2 column 20
Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi
Try this , if it works mark as
public class CheckRecurisive{
public static boolean check = true;
public static boolean runOnce(){
  if(check){
check = !check;
return true;
}
else{
return flase;
}
}
}

// your trigger updated
trigger RolluplineitemTrigger on QuoteLineItem  (after insert, after update) {
if(CheckRecurisive.runOnce()){
 List<Id> VQuoteID = new List<Id>();
    
    for(QuoteLineItem p: Trigger.new) {
        VQuoteID.add(p.QuoteiD);
    }
    
    AggregateResult[] groupedResults = [SELECT Sum(Stock_Not_Available__c) lineItemCOunt, QuoteiD FROM QuoteLineItem 
    WHERE QuoteiD in :VQuoteID  GROUP BY QuoteiD];
    
    Map<id,Quote> QuoteMap = new Map<Id,Quote>([SELECT id, No_of_Products_Stock_Not_Available__c 
    FROM Quote WHERE Id in :VQuoteID]); 
    
    for(AggregateResult ar: groupedResults) {
    
    QuoteMap.get((Id)ar.get('QuoteiD')).No_of_Products_Stock_Not_Available__c = (decimal) ar.get('lineItemCOunt');

       
    }
    
    //try {
        update QuoteMap.values();
    //}   catch(DmlException e) {
        //System.debug(e.getMessage());
    //}
}
​}
This was selected as the best answer
shakila Gshakila G
Thanks its working fine
shakila Gshakila G
Hi Deepak, Do we need to write a test class for this recursive class?
Lakshmi Deepak InkurthiLakshmi Deepak Inkurthi

Yes , it just you need to call Method from recursive class.

 

for your point , we need to write for all Apex Classes and Triggers