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
sandy hellosandy hello 

How to pass query with where clause in start method in batch class?

I have a requirent to pass query with where classs in start method in batch class to test few records. I am hard coding name value and executing it in anyonymous window. But the query is failing how can pass it. Similarly i must able to query based on id value. How can i achieve this

global database.querylocator start(Database.BatchableContext BC){ 
    query = ' select id,name from service where name=:test';
Suraj Tripathi 47Suraj Tripathi 47
Hi Sandy,

please check below example 
global class batchClassToUpdateTrailheadData implements Database.Batchable<sObject>,Database.Stateful {
    global integer total = 0;
    global integer success = 0;
    global integer failure = 0;    
    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('select ModuleName__c,(select name from Moduless__r where pickList__c = \'Completed\') from TrailHeadData__c ');
    global void execute(Database.BatchableContext bc,list<TrailHeadData__c> thdList){
        for(TrailHeadData__c thdObj : thdList){
                thdObj.ModuleName__c = thdObj.Moduless__r[0].name;
        Database.SaveResult[] res = Database.update(thdList,false);
        for(integer i = 0;i<res.size();i++){
            Database.SaveResult resObj = res.get(i);
                success += 1;
                failure += 1;
            total += 1;
    global void finish(Database.BatchableContext bc){
        system.debug('Total Processed records ==> '+total+' Total success records ==> '+success+' Total faliure records ==> '+failure);

If you find your Solution then mark this as the best answer.

Thank you!


Suraj Tripathi