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
Nordine BensadiaNordine Bensadia 

Using custom label with apex

Hi all,

Please find below a sample of a batch apex code I am trying to update :;

global class BatchDeleteEmailAttachV2 implements Database.Batchable<sObject> {
    
  global Database.QueryLocator start(Database.BatchableContext BC){
        
        String query = 'SELECT Id FROM Attachment where Parent.Type = \'EmailMessage\' AND CreatedDate > 2016-01-01T00:00:00.000+0000 AND CreatedDate < 2017-01-01T00:00:00.000+0000';
       return Database.getQueryLocator(query);
     }
    
global void execute(Database.BatchableContext BC,List<sObject> attachments){
    Database.delete(attachments);
     }

This is working correctly, however I would like to use custome labels, so the date range can easily be updated. Here is what I have tried but the 

global class BatchDeleteEmailAttachV2 implements Database.Batchable<sObject> {
    
  global Database.QueryLocator start(Database.BatchableContext BC){
        
        String query = 'SELECT Id FROM Attachment where Parent.Type = \'EmailMessage\' AND CreatedDate > Label.BatchDeleteEmailAttach_FROM AND CreatedDate < Label.BatchDeleteEmailAttach_TO';
       return Database.getQueryLocator(query);
     }
    
global void execute(Database.BatchableContext BC,List<sObject> attachments){
    Database.delete(attachments);
     }

This is working with the value (2016-01-01T00:00:00.000+0000) but not with the custom label (Label.BatchDeleteEmailAttach_FROM).

Can anyone help please?

Thanks,

Nordine.
Best Answer chosen by Nordine Bensadia
Maharajan CMaharajan C
Hi Nordine,

Please use the below change in Query String:

global class BatchDeleteEmailAttachV2 implements Database.Batchable<sObject> {
    
  global Database.QueryLocator start(Database.BatchableContext BC){

String query = 'SELECT Id FROM Attachment where Parent.Type = \'EmailMessage\' AND CreatedDate > ' + Label.BatchDeleteEmailAttach_FROM +' AND CreatedDate < ' + Label.BatchDeleteEmailAttach_TO;

       return Database.getQueryLocator(query);
     }
    
global void execute(Database.BatchableContext BC,List<sObject> attachments){
    Database.delete(attachments);
     }

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Nordine,

Please use the below change in Query String:

global class BatchDeleteEmailAttachV2 implements Database.Batchable<sObject> {
    
  global Database.QueryLocator start(Database.BatchableContext BC){

String query = 'SELECT Id FROM Attachment where Parent.Type = \'EmailMessage\' AND CreatedDate > ' + Label.BatchDeleteEmailAttach_FROM +' AND CreatedDate < ' + Label.BatchDeleteEmailAttach_TO;

       return Database.getQueryLocator(query);
     }
    
global void execute(Database.BatchableContext BC,List<sObject> attachments){
    Database.delete(attachments);
     }

Thanks,
Maharajan.C
This was selected as the best answer
Nordine BensadiaNordine Bensadia
Hi Maharajan.C,

This is working!!! Thank you so much!!! <3

Nordine.
Maharajan CMaharajan C

Thanks Nordine. Please mark the best answer !!!
Nordine BensadiaNordine Bensadia
Done! ;)