Skip to main content
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';

 
1 risposta
  1. 30 set 2021, 17:30
    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){

    if(thdObj.Moduless__r.size()>0){

    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);

    if(resObj.isSuccess()){

    success += 1;

    }

    else{

    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!

    Regards,

    Suraj Tripathi  

     
0/9000