+ Start a Discussion
Jayaramu T 9Jayaramu T 9 

I need to avoid SOQL query inside for loop but it is using for loop reference variable any suggetions please to avoid soql in loop below is the code


public Pricebook2 theBook {get;set;}
public List<Authorized_Product__c> authorizedProductObjList =[SELECT Active__c,Distribution_Type__c,Id,Name,Product_Category__c,Product_Collection__c,Price_Book__c,
                                    Product_Family__c,Reseller__c,Standard_Discount__c,Demo_Unit_Discount__c ,Parts_Discount__c,Material_Discount__c
                                    FROM Authorized_Product__c WHERE Reseller__c = :theOpp.RapidForm_Reseller__c AND Active__c = true];
for(Authorized_Product__c authp : authorizedProductObjList){
                
                if(PPP && authp.Product_Family__c.contains('Printers') && authp.Price_Book__c!=null){
                    theBook = [select Id, Name from Pricebook2 where ID = :authp.Price_book__c];
                    forcePricebookSelection=false;
                }
            }
        }
SFDC Beginner745SFDC Beginner745
Hi Jayaramu,

Please find the below code as per your requirement : 

 public Pricebook2 theBook {get;set;}
    public List<Authorized_Product__c> authorizedProductObjList =[SELECT Active__c,Distribution_Type__c,Id,Name,Product_Category__c,Product_Collection__c,Price_Book__c,
                                                                  Product_Family__c,Reseller__c,Standard_Discount__c,Demo_Unit_Discount__c ,Parts_Discount__c,Material_Discount__c
                                                                  FROM Authorized_Product__c WHERE Reseller__c = :theOpp.RapidForm_Reseller__c AND Active__c = true];
    theBook = [select Id, Name from Pricebook2];
    for(Authorized_Product__c authp : authorizedProductObjList){
        
        if(PPP && authp.Product_Family__c.contains('Printers') && authp.Price_Book__c!=null){
            if(theBook.id==authp.Price_book__c){
                forcePricebookSelection=false;
                
            }
            
        }
    }
}

Thanks
Salesforce Developer Group
Steven NsubugaSteven Nsubuga
public Pricebook2 theBook {get;set;}
public List<Authorized_Product__c> authorizedProductObjList =[SELECT Active__c,Distribution_Type__c,Id,Name,Product_Category__c,Product_Collection__c,Price_Book__c,
                                    Product_Family__c,Reseller__c,Standard_Discount__c,Demo_Unit_Discount__c ,Parts_Discount__c,Material_Discount__c
                                    FROM Authorized_Product__c WHERE Reseller__c = :theOpp.RapidForm_Reseller__c AND Active__c = true];

Set<ID>	ids = new Set<ID>();						
for(Authorized_Product__c authp : authorizedProductObjList){
                
                if(PPP && authp.Product_Family__c.contains('Printers') && authp.Price_Book__c!=null){
					ids.add(authp.Price_book__c);
                }
}
List<Pricebook2> theBooks = [select Id, Name from Pricebook2 where ID IN :ids];

 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
This version makes use of the Price_Book__r parent relationship to query the PriceBook as part of the Authorized_Product__c record.

<pre>
public Pricebook2 theBook { get; set; }
public List<Authorized_Product__c> authorizedProductObjList =
(   [   SELECT  Id, Name, Active__c, Distribution_Type__c, Product_Category__c,
                Product_Collection__c,
                Product_Family__c, Reseller__c, Standard_Discount__c,
                Demo_Unit_Discount__c, Parts_Discount__c, Material_Discount__c,
                Price_Book__c, Price_Book__r.Id, Price_Book__r.Name
        FROM    Authorized_Product__c
        WHERE   (   Reseller__c = :theOpp.RapidForm_Reseller__c
                AND Active__c = true
                )
    ]
);

    for ( Authorized_Product__c authp : authorizedProductObjList )
    {
        if  (   PPP
            &&  authp.Product_Family__c.contains( 'Printers' )
            &&  authp.Price_Book__c != null
            )
        {
            theBook = authp.Price_book__r;
            forcePricebookSelection = false;
        }
    }
</pre>
Jayaramu T 9Jayaramu T 9
Thanks Glyn and Steven