Hi Shri Raj, For your question, you can use the below code.
global class BatchExample implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'select id from Account where createDdate < LAST_N_DAYS:10';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Account> accountList){
if(accountList.size>0)
delete accountList;
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
And for more date literals you can refer this guide:- > https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
In case you find any other issue please mention. If you find your Solution then mark this as the best answer. Thanks and Regards Suraj Tripathi.
global class ExampleBatchClass implements Database.Batchable<sObject>{
global ExampleBatchClass(){
// Batch Constructor
}
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
Date s = system.today()-10;
string query = 'select id from Account where createDdate <:S';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Account>scope){
if(scope.size>0)
delete scope;
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
I realize this is an older thread, but I wanted to respond to the previous comment. LAST_N_DAYS:10 means "a range of datetimes that includes all of the last ten days" and can be referenced with "=" because it is a range, not a single date.
The previous comment raises a good point that you can use LAST_N_DAYS in queries, but it is incorrect and would actually return all Accounts in the database. Probably a typo, but in case anyone is using this article in the future, hopefully this helps:
//Returns Id's for all Account records created during the last 10 days
Select Id from Account Where CreatedDate = LAST_N_DAYS:10
//Returns Id's for all Account records created BEFORE the last 10 days
Select Id from Account Where CreatedDate < LAST_N_DAYS:10
//Returns Id's for all Account records in the database: all that were created in the last 10 days AND all that were created before the last ten days
Select Id from Account Where CreatedDate <= LAST_N_DAYS:10
Hi Shri Raj, For your question, you can use the below code.
global class BatchExample implements Database.Batchable<sObject>{
// Start Method
global Database.QueryLocator start(Database.BatchableContext BC){
string query = 'select id from Account where createDdate < LAST_N_DAYS:10';
return Database.getQueryLocator(query);
}
// Execute Logic
global void execute(Database.BatchableContext BC, List<Account> accountList){
if(accountList.size>0)
delete accountList;
}
global void finish(Database.BatchableContext BC){
// Logic to be Executed at finish
}
}
And for more date literals you can refer this guide:- > https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
In case you find any other issue please mention. If you find your Solution then mark this as the best answer. Thanks and Regards Suraj Tripathi.
For your question, you can use the below code.
And for more date literals you can refer this guide:-
> https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Thanks and Regards
Suraj Tripathi.
All Answers
You can use "Select Id From Account Where CreatedDate <= LAST_N_DAYS:10"
Also, you don't need the "if(scope.size > 0)" check at line 16. It's superfluous because the DML will only run on non-empty or populated lists.
The previous comment raises a good point that you can use LAST_N_DAYS in queries, but it is incorrect and would actually return all Accounts in the database. Probably a typo, but in case anyone is using this article in the future, hopefully this helps:
It was an older thread.Howeve,your suggestion regarding queries helped me.
Thanks
For your question, you can use the below code.
And for more date literals you can refer this guide:-
> https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_dateformats.htm
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Thanks and Regards
Suraj Tripathi.