I'm trying to get a count of how many placements are on an account. If the record type is subcontract, I want the query to look at the subcontract field on the placement and if the account record type is anything else, I would like it to look at the client field on the placement. I think I have a good solution but it returns with "Batch - too many SOQL Queries 201".
Batch - too many SOQL Queries 201.
I know I need to remove the SOQL queries from the for loop, but I'm unsure how to get the same result otherwise.
public class plcCounterOnAcct implements Database.Batchable<sObject>, Database.Stateful { public Database.QueryLocator start(Database.BatchableContext bc) { String query = 'SELECT Id, Name FROM Account'; return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, List<Account> acctList){ for(Account acc : acctList){ List<ts2__Placement__c> plcOnSubConAcct = [SELECT Id FROM ts2__Placement__c WHERE Subcontract_Company__c = :acc.Id]; List<ts2__Placement__c> plcOnAcct = [SELECT Id FROM ts2__Placement__c WHERE ts2__Client__c = :acc.Id]; if(plcOnSubConAcct.size() > 0){ acc.Total_Placement_Count__c = plcOnSubConAcct.size(); }else{acc.Total_Placement_Count__c = plcOnAcct.size(); } } try { // Update the Account Record update acctList; } catch(Exception e) { System.debug(e); } } public void finish(Database.BatchableContext BC){ } }