You need to sign in to do that
Don't have an account?
Nordine 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.
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.
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
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 is working!!! Thank you so much!!! <3
Nordine.
Thanks Nordine. Please mark the best answer !!!