I have developed following code for apex trigger and it will work when i am going to insert bulk data from the data loader,
Trigger: System.LimitException: Too many SOQL queries: 101
I read some post for bulkify and best practice to write apex trigger but in below code i mentioned i am not sure how can i bulkified it.
please guide me.
here is my code :
List<SBQQ__QuoteLine__c> quoteLineList = new List<SBQQ__QuoteLine__c>();
for(opportunity oppAfter : trigger.New){
List<Product2> prList = [select Id, Name from Product2 where Name =: oppAfter.Quote_line_Product_Name__c Limit 1];
if(oppAfter.Coming_from_Portal__c == True){
if(trigger.isAfter){
SBQQ__Quote__c quote = new SBQQ__Quote__c();
quote.SBQQ__Account__c = oppAfter.AccountId;
quote.SBQQ__PriceBook__c = [Select Id from Pricebook2 where Name = 'Lista de Precios CPQ' Limit 1].Id;
quote.Primary_Contact__c = [Select Id from Contact where AccountId =: oppAfter.AccountId Order By createdDate Limit 1].Id;
insert quote;
SBQQ__QuoteLine__c qliNew = new SBQQ__QuoteLine__c();
qliNew.SBQQ__Product__c = [Select Id, Name from Product2 where Name =: 'DATA NETWORK SERVICES' Limit 1].Id;
qliNew.SBQQ__Quote__c = quote.Id;
insert qliNew;
for(SBQQ__Quote__c q : [select Id from SBQQ__Quote__c where Id =: quote.Id]){
if(oppAfter.Quote_line_Product_Name__c != Null){
SBQQ__QuoteLine__c qli = new SBQQ__QuoteLine__c();
for(Product2 p : prList){
qli.SBQQ__Product__c = p.Id;
if(oppAfter.Product_Special_Price_coming_from_portal__c != Null){
qli.SBQQ__SpecialPrice__c = Decimal.valueOf(oppAfter.Product_Special_Price_coming_from_portal__c);
}
qli.SBQQ__Quote__c = q.Id;
qli.SBQQ__SpecialPriceType__c = 'Custom';
if(oppAfter.Quote_line_TipoClearChanel_Velocidad__c != Null){
qli.TipoClearChanel__c = String.ValueOf(oppAfter.Quote_line_TipoClearChanel_Velocidad__c);
}
quoteLineList.add(qli);
}
}
}
}
}
}
if(quoteLineList.size() > 0)
insert quoteLineList;
}
for(SBQQ__Quote__c q : [select Id from SBQQ__Quote__c where Id =: quote.Id]){