function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DbjensenDbjensen 

help...Too Many SOQL Queries

hello - I'm hitting a govenor limit with this SOQL query. I can't figure out why it's hitting the limit. Any help would be greatly appreciated. 

for(Lead newLds : newLead){
            mapOfNewAffinityType.put(newLds.Affinity_Type__c, newLds.LastNameStreetAndZipCode__c);
            mapOfNewAffinitySubType.put(newLds.Affinity_Sub_Type__c, newLds.LastNameStreetAndZipCode__c);
        }

//Query Affinity Type 
        List<Affinity__c> affinity = [SELECT Id, Occupation_Code__c, Occupation_Sub_Type_Code__c, Occupation__c,
                                      Occupation_Sub_Type__c FROM Affinity__c
                                      WHERE Occupation_Code__c = :mapOfNewAffinityType.keySet() OR
                                      Occupation_Sub_Type_Code__c = :mapOfNewAffinitySubType.keySet()];

//Affinity maps
        Map<String, String> mapAffinityTyp = new Map<String, String>();
        Map<String, String> mapAffinitySubTyp = new Map<String, String>();
        
        //Affinity values
        for(Affinity__c af : affinity) {
            mapAffinityTyp.put(af.Occupation_Code__c, af.Occupation__c);
            mapAffinitySubTyp.put(af.Occupation_Sub_Type_Code__c, af.Occupation_Sub_Type__c);
        }
Andrew GAndrew G
Try adjusting your query to use IN rather than equals:
 
List<Affinity__c> affinity = [SELECT Id, Occupation_Code__c, Occupation_Sub_Type_Code__c, Occupation__c,
                                      Occupation_Sub_Type__c FROM Affinity__c
                                      WHERE Occupation_Code__c IN :mapOfNewAffinityType.keySet() OR
                                      Occupation_Sub_Type_Code__c IN :mapOfNewAffinitySubType.keySet()];

Regards
Andrew
DbjensenDbjensen
Hi Andrew - Thanks for catching that. I made this change but unfortunately, I'm still getting the error. Any other thoughts to fix this issue?  
Andrew GAndrew G
Curious.
The two things I would try now would be:
1. Strip out one condition in the WHERE statement to see the result.
2. Strip out the other condition in the WHERE statement to see the result
3. With the full WHERE condition, only pull the Id in the SELECT statement.

Apart from that i would need to do a setup in a test environment to investigate.

Regards
Andrew