You need to sign in to do that
Don't have an account?
Christos Kolonis
How to avoid using soql inside for loop
Hello,
I have the below code where i need to use a for loop to iterate into the pConf list. I need to avoid using the soql queries ( pConfAttr , cProd ) any ideas of how could i avoid it?
Thank you!
I have the below code where i need to use a for loop to iterate into the pConf list. I need to avoid using the soql queries ( pConfAttr , cProd ) any ideas of how could i avoid it?
Thank you!
public list<String> getBasketEnergyInfo(){ cscfga__Product_Basket__c pBask = [select id from cscfga__Product_Basket__c where Id =:pBasket.Id limit 1]; list<cscfga__Product_Configuration__c> pConf = [select cscfga__Product_Basket__c from cscfga__Product_Configuration__c where cscfga__Product_Basket__c =: pBask.Id]; list<cscfga__Attribute__c> pConfAttr = [select name, cscfga__Value__c, cscfga__Product_Configuration__c, cssecmp__attribute_type__c from cscfga__Attribute__c where cscfga__Product_Configuration__c =: pConf[0].id AND cssecmp__attribute_type__c = 'Lookup']; } list<Id> priceItemId = new list<Id>(); for(cscfga__Attribute__c confVal: pConfAttr){ if(confVal.cscfga__Value__c != null){ id lookup = confVal.cscfga__Value__c; String sObjName = lookup.getSObjectType().getDescribe().getName(); if(sObjName == 'cspmb__Price_Item__c'){ priceItemId.add(lookup); } } } system.debug(priceItemId); cspmb__Price_Item__c cProd = [select id, el_Product_Type__c, cspmb__Product_Definition_Name__c from cspmb__Price_Item__c where id =: priceItemId]; list<String> productTypesInfo = new list<String>(); String productType = cProd.el_Product_Type__c; //Current basket's Product Type (Residential or Business) String productEnergy = cProd.cspmb__Product_Definition_Name__c; //Current basket's Energy Type (Electricity or Gas) productTypesInfo.add(productType); productTypesInfo.add(productEnergy); return productTypesInfo; }
To understand how to avoid using SOQL inside for loop, I suggest taking a look at this example
I added a for loop to indicate how i need this to become. But unfortunately 2 soql queries will exist inside that for loop.