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
Mahesh Babu 187Mahesh Babu 187 

Need help in bulkifying a trigger code

Hi Team,

I have written a trigger. It is working fine when the quote lines are less but when the quote lines are increased I am getting error.
Please help me in bulkifying this code:

APEX CODE:

trigger SpendDiscountCalc on SBQQ__QuoteLine__c (before insert, before update) {
    
    Decimal tierAmount = 3000000;
    Decimal percent1 = 0.00;
    Decimal percent2 = 0.12;
    Decimal percent3 = 0.14;
    Decimal percent4 = 0.16;
    Decimal percent5 = 0.18;
    Decimal percent6 = 0.20;
    Decimal tierdiscountamt1 = 0.00;
    Decimal tierdiscountamt2 = 0.00;
    Decimal tierdiscountamt3 = 0.00;
    Decimal tierdiscountamt4 = 0.00;
    Decimal tierdiscountamt5 = 0.00;
    Decimal tierdiscountamt6 = 0.00;
    Decimal pct1 = 0.00;
    Decimal pct2 = 0.00;
    Decimal pct3 = 0.00;
    Decimal pct4 = 0.00;
    Decimal pct5 = 0.00;
    Decimal pct6 = 0.00;
    
    for(SBQQ__QuoteLine__c quoteLine : Trigger.new){
        
        if((quoteLine.Quote_Avg_Net_Price__c!=0 && quoteLine.Account_Name__c!=null && quoteLine.SBQQ__Quote__c != null && quoteLine.Eligible_for_Spend_Discount__c == true && quoteline.Lightbox_Pricing__c == true)
           && (quoteLine.Quote_Avg_Net_Price__c > tierAmount))
        {   
            
            System.debug('quoteLine.Quote_Avg_Net_Price__c: '+quoteLine.Quote_Avg_Net_Price__c); 
            Decimal tierdiscountamt1 = quoteLine.Quote_Avg_Net_Price__c - tierAmount;
            System.debug('tierdiscountamt1 :'+tierdiscountamt1); 
            Decimal pct1 = (tierAmount * percent1);  
            System.debug('pct1: '+pct1); 
            
            if(tierdiscountamt1 > tierAmount ){
                Decimal tierdiscountamt2 = tierdiscountamt1 - tierAmount;
                System.debug('tierdiscountamt2 :'+tierdiscountamt2); 
                Decimal pct2 = (tierAmount * percent2);
                System.debug('pct2: '+pct2); 
            }
            else
            {
                Decimal pct2 = (tierdiscountamt1 * percent2);
                System.debug('pct2: '+pct2);
                
                if(tierdiscountamt2 > tierAmount){
                    Decimal tierdiscountamt3 = tierdiscountamt2 - tierAmount; 
                    System.debug('tierdiscountamt3 :'+tierdiscountamt3); 
                    Decimal pct3 = (tierAmount * percent3);
                    System.debug('pct3: '+pct3); 
                }
                else{
                    Decimal pct3 = (tierdiscountamt2 * percent3);
                    
                    if(tierdiscountamt3 > tierAmount){
                        Decimal tierdiscountamt4 = tierdiscountamt3 - tierAmount;
                        System.debug('tierdiscountamt4 :'+tierdiscountamt4);
                        Decimal pct4 = (tierAmount * percent4);
                        System.debug('pct4: '+pct4); 
                    }
                    else{
                        Decimal pct4 = (tierdiscountamt3 * percent4);
                        if(tierdiscountamt4 > tierAmount){
                            Decimal tierdiscountamt5 = tierdiscountamt4 - tierAmount;
                            System.debug('tierdiscountamt5 :'+tierdiscountamt5); 
                            Decimal pct5 = (tierAmount * percent5); 
                            System.debug('pct5: '+pct5); 
                        }
                        else{
                            Decimal pct5 = (tierdiscountamt4 * percent5); 
                            if(tierdiscountamt5 > tierAmount){
                                Decimal tierdiscountamt6 = tierdiscountamt5 - tierAmount;
                                System.debug('tierdiscountamt6 :'+tierdiscountamt6);
                                Decimal pct6 = (tierdiscountamt6 * percent6);
                                System.debug('pct6: '+pct6);
                            }
                            else{
                                Decimal pct6 = (tierdiscountamt5 * percent6);  
                                quoteLine.Spend_Discount__c = (((pct1+pct2+pct3+pct4+pct5+pct6) / quoteLine.Quote_Avg_Net_Price__c) * 100);    
                                System.debug('quoteLine.Spend_Discount__c: '+quoteLine.Spend_Discount__c);
                            }
                        }                        
                    }
                }
            }
                                
        }        
        else{
            quoteLine.Spend_Discount__c = 0;
        }
        
    } 
}

Thank you,
Mahesh
ShirishaShirisha (Salesforce Developers) 
Hi Mahesh,

Greetings!

I would suggest you to work on the code based on the error message thrown.Please go through the below blog to bulkify the trigger code:

https://force-base.com/2016/02/03/how-to-bulkify-trigger-in-salesforce-step-by-step-guide/

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri