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
Sunny Solanki 11Sunny Solanki 11 

Batch fail with caused by: System.QueryException: Variable does not exist: Label.CustomObject_Record_Type

Hi Gurus, 
I have a batch class where I am using the custom label to limit result by record type and doing a blank update on the result.  Batch class has been scheduled and failing with: Source organization: ****************8(null) Failed to process batch for class 'BatchBlankUpdateCompliance' for job id '7074D00000LzJIL'.  Please, can anyone assist me the correct this error?  
 
caused by: System.QueryException: Variable does not exist: Label.PSG_Compliance_Record_Type
 
Class.BatchBlankUpdateCompliance.start: line 7, column 1

global class BatchBlankUpdateCompliance implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID FROM Compliance__c ' + 
            'Where RecordTypeId =: Label.CustomObject_Record_Type'
        );
    }
   global void execute(Database.BatchableContext bc, List<Compliance__c> scope){
        // process each batch of records
        List<Compliance__c> compList = New List<Compliance__c>();
        for (Compliance__c comp : scope) {
            compList.add(comp);
            recordsProcessed = recordsProcessed + 1;
            }
        update compList;
    }    
   global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');


 
Best Answer chosen by Sunny Solanki 11
Raj VakatiRaj Vakati
Modify query as shown below 
 
return Database.getQueryLocator(
            'SELECT ID FROM Compliance__c ' + 
            'Where RecordTypeId =\''+ Label.CustomObject_Record_Type+'\''
        );

Complete Code
 
global class BatchBlankUpdateCompliance implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID FROM Compliance__c ' + 
            'Where RecordTypeId =\''+ Label.CustomObject_Record_Type+'\''
        );
    }
   global void execute(Database.BatchableContext bc, List<Compliance__c> scope){
        // process each batch of records
        List<Compliance__c> compList = New List<Compliance__c>();
        for (Compliance__c comp : scope) {
            compList.add(comp);
            recordsProcessed = recordsProcessed + 1;
            }
        update compList;
    }    
   global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');

 

All Answers

Raj VakatiRaj Vakati
Modify query as shown below 
 
return Database.getQueryLocator(
            'SELECT ID FROM Compliance__c ' + 
            'Where RecordTypeId =\''+ Label.CustomObject_Record_Type+'\''
        );

Complete Code
 
global class BatchBlankUpdateCompliance implements 
    Database.Batchable<sObject>, Database.Stateful {
    
    // instance member to retain state across transactions
    global Integer recordsProcessed = 0;
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID FROM Compliance__c ' + 
            'Where RecordTypeId =\''+ Label.CustomObject_Record_Type+'\''
        );
    }
   global void execute(Database.BatchableContext bc, List<Compliance__c> scope){
        // process each batch of records
        List<Compliance__c> compList = New List<Compliance__c>();
        for (Compliance__c comp : scope) {
            compList.add(comp);
            recordsProcessed = recordsProcessed + 1;
            }
        update compList;
    }    
   global void finish(Database.BatchableContext bc){
        System.debug(recordsProcessed + ' records processed. Shazam!');

 
This was selected as the best answer
Sunny Solanki 11Sunny Solanki 11
Thank you, Raj.  your workaround looks better than mine. I have used the following workaround. Thank you again, Raj for your quick response and support. 

// instance member to retain state across transactions
05    global Integer recordsProcessed = 0;
//added line:  
       String abc = Label.CustomObject_Record_Type;
06    global Database.QueryLocator start(Database.BatchableContext bc) {
07        return Database.getQueryLocator(
08            'SELECT ID FROM Compliance__c ' +
09            'Where RecordTypeId =abc'
10        );
 
Raj VakatiRaj Vakati
Try like below and mark it as solved 
 
String abc = Label.CustomObject_Record_Type;
06    global Database.QueryLocator start(Database.BatchableContext bc) {
07        return Database.getQueryLocator(
08            'SELECT ID FROM Compliance__c ' +
09            'Where RecordTypeId =\'+'abc'+'\'''
10        );