+ Start a Discussion
Anirudh Sharma 5Anirudh Sharma 5 

SOQL 101 error

Can you help me out with the error that I am getting I am pasting the Class that I wrote so when I click a button called review it says too many SOQL quiries.I have highlited the Query can you please help me with this 

public static void checkTier( Advisory_Panel_Application__c advisoryp,Advisory_Panel_Application_Review__c advisorypanels){
        Id recordTypeId = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advsiory_Panel_on_Addressing_Disparities).getRecordTypeId();
        Id recordTypeId1 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advisory_Panel_on_Clinical_Trials).getRecordTypeId(); 
        Id recordTypeId2 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advisory_Panel_on_APDTO).getRecordTypeId(); 
        Id recordTypeId3 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advisory_Panel_on_Improving_Healthcare_System).getRecordTypeId(); 
        Id recordTypeId4 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advisory_Panel_on_Rare_Disease).getRecordTypeId(); 
        Id recordTypeId5 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advsiory_Panel_on_Communication_and_Dissemination_Research).getRecordTypeId(); 
        Id recordTypeId6 = Schema.SObjectType.Advisory_Panel_Application_Review__c.getRecordTypeInfosByName().get(Label.Tier_2_Review_Advsiory_Panel_on_Patient_Engagement).getRecordTypeId(); 
        String addressing =Label.Addressing_Disparities;
        String apdto=Label.APDTO; 
        String clinical=Label.Clinical_Trials;
        String improvinghs=Label.Improving_Healthcare_Systems;
        String rared=Label.Rare_Disease;
        String commdere=Label.Communication_and_Dissemination_Research;
        String patiente=Label.Patient_Engagement;
        String pass =Label.Pass;
        String fail=Label.Fail;
        String yes=Label.Yes;
        String maybe=Label.Maybe;
        String noo=Label.No;
        String alternate =Label.Alternate;
         String adminname=Label.AdminUser;
 String tier1name=Label.Tier_1_name;
        String tier1name1=Label.Tier_1_name2;
        String tier2name=Label.Tier_2_name;
        String tier2name1=Label.Tier_2_name1;
        String tier1Status=Label.Tier_1;
        String tier1SubStatus=Label.Tier_1_Under_Review;
        String tier1SubStatusHold=Label.Tier_1_Hold;
        String tier2Status=Label.Tier_2;
        String tier2SubStatus=Label.Tier_2_Under_Review;
        String tier2SubStatusHold=Label.Tier_2_Hold;
        String tier3Status=Label.Tier_3;
        String tier3SubStatus=Label.Tier_3_Under_Review;
        
            User usr=[Select id, name from User where name=:adminname limit 1];  
        // U- returns the user id of the User with name 'PCORI SF Administrator'
        Advisory_Panel_Application_Review__c advisoryr1 = [select id, Review_Name__c,Review_Complete__c,Tier_1_Tag__c,Advisory_panel_s_applied_to__c,OwnerId,Advisory_Panel_Application__c,Advisory_Panel__c,Owner.Name from Advisory_Panel_Application_Review__c where Advisory_Panel_Application__c=:advisorypanels.Advisory_Panel_Application__c and Review_Name__c =:tier1name and Advisory_Panel__c=:advisorypanels.Advisory_Panel__c limit 1];
        // ap - Returns the Advisory Panel Application Review record for Tier 1 Review 1 with the details queried in the SOQL
        Advisory_Panel_Application_Review__c advisoryr2 = [select id, Review_Name__c,Review_Complete__c,Tier_1_Tag__c,Advisory_panel_s_applied_to__c,OwnerId,Advisory_Panel_Application__c,Advisory_Panel__c,Owner.Name from Advisory_Panel_Application_Review__c where Advisory_Panel_Application__c=:advisorypanels.Advisory_Panel_Application__c and Review_Name__c=:tier1name1 and Advisory_Panel__c=:advisorypanels.Advisory_Panel__c limit 1]; 
   
     // ap1 - Returns the Advisory Panel Application Review record for Tier 1 Review 2 with the details queried in the SOQL
        if((advisorypanels.Tier_2_Tag__c != yes && advisorypanels.Tier_2_Tag__c != noo && advisorypanels.Tier_2_Tag__c != alternate ) && advisoryr1.Review_Complete__c == true && advisoryr2.Review_Complete__c==true){
            if((advisoryr1.Tier_1_Tag__c == yes|| advisoryr1.Tier_1_Tag__c== Maybe) && (advisoryr2.Tier_1_Tag__c == yes ||advisoryr2.Tier_1_Tag__c == Maybe)){
 
pconpcon
This is most likely not occuring directly because of this class, but any classes and queries leading up to this class.  There are somethings you can do to reduce your queires [1].  For this specifically you can combine these two queries into a single query and that will help.
 
Set<String> tierNames = new Set<String> {
    tier1name,
    tier1name1
};

Advisory_Panel_Application_Review__c advisoryr1;
Advisory_Panel_Application_Review__c advisoryr2;

for (Advisory_Panel_Application_Review__c advisory : [
    select Advisory_Panel_Application__c,
        Advisory_Panel__c,
        Advisory_panel_s_applied_to__c,
        Owner.Name
        OwnerId,
        Review_Complete__c,
        Review_Name__c,
        Tier_1_Tag__c,
    from Advisory_Panel_Application_Review__c
    where Advisory_Panel_Application__c = :advisorypanels.Advisory_Panel_Application__c and
        Review_Name__c in :tierNames and
        Advisory_Panel__c = :advisorypanels.Advisory_Panel__c
]) {
    if (advisory.Review_Name__c == tier1name) {
        advisoryr1 = advisory;
    } else if (advisory.Review_Name__c == tier1name2) {
        advisoryr2 = advisory;
    }   
}
NOTE: This code has not been tested and may contain typographical or logical errors

[1] http://blog.deadlypenguin.com/blog/2012/04/04/reducing-salesforce-soql-queries-by-using-static-variables/