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
Luke Higgins 23Luke Higgins 23 

Batch Class Error on Execute

I am trying to count the number of placements on each account. I thought about using a trigger and flow to do this but I think there are too many placements for this to work. This batch class I wrote is returning an error that I am uncertain how to get around. "Invalid bind expression type Account for Column of type Id" on line 9
 
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> plcOnAcct = [SELECT Id FROM ts2__Placement__c WHERE ts2__Client__c = :acc];
            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){
    }
}

 
SarvaniSarvani
Hi Luke,

What does your field "ts2__Client__c " store ?   If it has AccountId value in it try changing your line 9 like below:
List<ts2__Placement__c> plcOnAcct = [SELECT Id,ts2__Client__c FROM ts2__Placement__c WHERE ts2__Client__c = :acc.Id];

This will compare if there are any ts2__Placement__c  records with field ts2__Client__c having AccountId.

Hope this helps!

Thanks