You need to sign in to do that
Don't have an account?
Mahesh Babu 187
Bulkification of Before Insert and Before Update Trigger
Hi Team,
I have a Apex class which is called in trigger. It is throwing Apex CPU time limit exceeded Error. I have searched and found that my code is not bulkified but I am facing issue in bulkifying it.
APEX CODE:
public class QuoteLineClass {
public static void UpdatePricingCategory(List<SBQQ__QuoteLine__c> qLine){
System.Debug('UpdatePricingCategory begin');
for(SBQQ__QuoteLine__c quoteLine : qLine) {
Id VantageRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Vantage').getRecordTypeId();
Id NonVanProdRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Non Vantage').getRecordTypeId();
if(quoteLine.SBQQ__Product__c!=null && (quoteLine.Offer_Name__c == null || quoteLine.Offer_Name__c == '')){
List<SBQQ__ContractedPrice__c> cps=[Select id,RecordTypeId,Pricing_Master_Version__c,SBQQ__Price__c,Pricing_Category__c,SBQQ__EffectiveDate__c,SBQQ__ExpirationDate__c, SBQQ__Product__c from SBQQ__ContractedPrice__c where SBQQ__Product__c =:quoteLine.SBQQ__Product__c
AND Status__c='Approved' AND SBQQ__EffectiveDate__c <=:quoteLine.Activity_Date__c AND SBQQ__ExpirationDate__c >=:quoteLine.Activity_Date__c];
System.Debug('1... SBQQ__ContractedPrice__c size' + cps.size());
for(SBQQ__ContractedPrice__c cp :cps){
if(cp.RecordTypeId == VantageRecordTypeId){
quoteLine.CP_Pricing_Category__c = cp.Pricing_Category__c;
quoteLine.CP_Vantage_Pricing_Version__c = cp.Pricing_Master_Version__c;
quoteLine.SBQQ__ContractedPrice__c = null;
quoteLine.SBQQ__SpecialPriceType__c = null;
quoteLine.CP_Lightbox_Pricing__c = true;
System.Debug('inside if');
}
else if(cp.RecordTypeId == NonVanProdRecordTypeId){
quoteLine.SBQQ__ContractedPrice__c = cp.Id;
quoteLine.CP_Lightbox_Pricing__c = false;
System.Debug('inside else if');
}
}
}
}
}
public static void UpdateQuoteLine(List<SBQQ__QuoteLine__c> qLine){
List<SBQQ__QuoteLine__c> q = new List<SBQQ__QuoteLine__c>();
for(SBQQ__QuoteLine__c quoteLine : qLine){
if(quoteLine.Score_Band__c == 'No Score Band'){
quoteLine.SBQQ__ContractedPrice__c = null;
quoteLine.SBQQ__SpecialPriceType__c = null;
}
}
}
}
TRIGGER:
trigger QuoteLineTrigger on SBQQ__QuoteLine__c (before insert, after insert, before update, after update, before delete, after delete) {
QuoteLineTriggerDispatcher dispatcher = new QuoteLineTriggerDispatcher();
dispatcher.dispatchEvent();
System.Debug('inside trigger before if');
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
if(checkRecursive.runOnce()){
System.Debug('inside trigger after if');
QuoteLineClass.UpdatePricingCategory(Trigger.new);
QuoteLineClass.UpdateQuoteLine(Trigger.new);
System.Debug('inside trigger after if.... completed');
}
}
}
Please help me in bulkifying it.
Thank You,
Mahesh
I have a Apex class which is called in trigger. It is throwing Apex CPU time limit exceeded Error. I have searched and found that my code is not bulkified but I am facing issue in bulkifying it.
APEX CODE:
public class QuoteLineClass {
public static void UpdatePricingCategory(List<SBQQ__QuoteLine__c> qLine){
System.Debug('UpdatePricingCategory begin');
for(SBQQ__QuoteLine__c quoteLine : qLine) {
Id VantageRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Vantage').getRecordTypeId();
Id NonVanProdRecordTypeId = Schema.SObjectType.SBQQ__ContractedPrice__c.getRecordTypeInfosByName().get('Non Vantage').getRecordTypeId();
if(quoteLine.SBQQ__Product__c!=null && (quoteLine.Offer_Name__c == null || quoteLine.Offer_Name__c == '')){
List<SBQQ__ContractedPrice__c> cps=[Select id,RecordTypeId,Pricing_Master_Version__c,SBQQ__Price__c,Pricing_Category__c,SBQQ__EffectiveDate__c,SBQQ__ExpirationDate__c, SBQQ__Product__c from SBQQ__ContractedPrice__c where SBQQ__Product__c =:quoteLine.SBQQ__Product__c
AND Status__c='Approved' AND SBQQ__EffectiveDate__c <=:quoteLine.Activity_Date__c AND SBQQ__ExpirationDate__c >=:quoteLine.Activity_Date__c];
System.Debug('1... SBQQ__ContractedPrice__c size' + cps.size());
for(SBQQ__ContractedPrice__c cp :cps){
if(cp.RecordTypeId == VantageRecordTypeId){
quoteLine.CP_Pricing_Category__c = cp.Pricing_Category__c;
quoteLine.CP_Vantage_Pricing_Version__c = cp.Pricing_Master_Version__c;
quoteLine.SBQQ__ContractedPrice__c = null;
quoteLine.SBQQ__SpecialPriceType__c = null;
quoteLine.CP_Lightbox_Pricing__c = true;
System.Debug('inside if');
}
else if(cp.RecordTypeId == NonVanProdRecordTypeId){
quoteLine.SBQQ__ContractedPrice__c = cp.Id;
quoteLine.CP_Lightbox_Pricing__c = false;
System.Debug('inside else if');
}
}
}
}
}
public static void UpdateQuoteLine(List<SBQQ__QuoteLine__c> qLine){
List<SBQQ__QuoteLine__c> q = new List<SBQQ__QuoteLine__c>();
for(SBQQ__QuoteLine__c quoteLine : qLine){
if(quoteLine.Score_Band__c == 'No Score Band'){
quoteLine.SBQQ__ContractedPrice__c = null;
quoteLine.SBQQ__SpecialPriceType__c = null;
}
}
}
}
TRIGGER:
trigger QuoteLineTrigger on SBQQ__QuoteLine__c (before insert, after insert, before update, after update, before delete, after delete) {
QuoteLineTriggerDispatcher dispatcher = new QuoteLineTriggerDispatcher();
dispatcher.dispatchEvent();
System.Debug('inside trigger before if');
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
if(checkRecursive.runOnce()){
System.Debug('inside trigger after if');
QuoteLineClass.UpdatePricingCategory(Trigger.new);
QuoteLineClass.UpdateQuoteLine(Trigger.new);
System.Debug('inside trigger after if.... completed');
}
}
}
Please help me in bulkifying it.
Thank You,
Mahesh
These lines are expensive, Please move them out of your for loop. Also I can see you have written SOQL inside for loop, It is a bad practice and probably will lead to 101 SOQL Error. Please move them out of your loop too.