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
Taylor YoungTaylor Young 

Invalid bind expression type of List<Id> for column of type Id

I am writing a batch that updates a custom object. There are millions of records though, and the query can't even be run without using a limit. Using the Query Plan tool I see that the Cost of querying this object is almost 6.  So instead I rewrote the batch to query the parent. The cost of the parent is about 4 (still very high, but at least the query works). 

In order to run the batch this way I have to query the parent object, create a list of their ids, and pass that list of ids to the new batch query. 

I am accomplishing that by doing the following: 
String schedQuery = 'select id FROM schedules__c '+ 
'WHERE DealProgram__c LIKE '%Gray%' AND Week__c >= :queryStart AND Week__c <= :queryEnd';
     

List<Schedules__C> schedules = Database.Query(schedQuery);


List<Id> schedIds = new List<Id>();
For(Schedules__c s : schedules){
    schedIds.add(s.Id);
}


And I can see in the logs that this is in fact a List of Ids and it is not empty. 


The following is the query that I pass to my batch: 
String query = 'select id, Air_Date__c, Order_Number__c, Invoice_Number__c  FROM spot_data__c ' + 
'WHERE schedule__c IN(:schedIds) AND ' + 
'Order_Number__c IN :orderNums ORDER BY Air_Date__c ASC ';
Even though the query works completely find in the Query Editor, I am getting the error message in the title which makes no sense to me: "Invalid bind expression type of List<Id> for column of type Id ". Most of the errors I saw like this are when 1.the type in the list is a mismatch (Case vs. Id) or 2. What is being passed in is a List<List<id>> instead of List<Id>. Neither of those are a problem here.

Below I will share the code in the batch. Thank you in advance for your generosity and time.

This is excluding the execute and finish methods because they are not reached before the error occurs:
 
global class InvoiceNumberSpotDataBatch implements Database.Batchable<sObject>{
    
    global final String query;
    global final Map<String, List<Map<String, String>>> orderMap;
    global final List<String> orderNums;
    global final List<Id> schedIds;
    global final Date queryStart;
    global final Date queryEnd;
    
   global InvoiceNumberSpotDataBatch(String query, Map<String, List<Map<String, String>>> orderMap, List<String> orderNums, Date queryStart, Date queryEnd, List<Id> schedIds ){
       	this.orderMap=orderMap;
       	this.orderNums=orderNums;
       	this.queryStart=queryStart;
       	this.queryEnd=queryEnd;
        this.query=query;
       	this.schedIds=schedIds;
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
   }
}

 
Best Answer chosen by Taylor Young
David Zhu 🔥David Zhu 🔥
You may change the way you built the queryString.

String idString = '\'' + String.join(schedIds,'\',\'') + '\'';

String query = 'select id, Air_Date__c, Order_Number__c, Invoice_Number__c FROM spot_data__c ' + 'WHERE schedule__c IN (' + idString + ') AND ' + 'Order_Number__c IN :orderNums ORDER BY Air_Date__c ASC ';