You need to sign in to do that
Don't have an account?
Ayyagari Ramesh
Batch Apex class to delete the reports which were created 6 months back and is unused
Hi all,
I have written batch class which will be sheduled to delete reports which were created 6 months and are not used. I am not sure if this is correct or is there anything required please need suggestion for this.
Global class BatchMassDeleteReports Implements Database.batchable<sobject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([SELECT Id from Report where CreatedDate <= LAST_N_DAYS:180 AND LastModifiedDate <= LAST_N_DAYS:180]);
}
global void execute(Database.BatchableContext BC,List<Report> scope){
list<Report> delReports = (list<Report>)scope;
if(delReports.CreatedDate <= LAST_N_DAYS:180 && delReports.LastModifiedDate <= LAST_N_DAYS:180)
{
delete delReports;
}
}
global void finish(Database.BatchableContext BC){
}
}
I have written batch class which will be sheduled to delete reports which were created 6 months and are not used. I am not sure if this is correct or is there anything required please need suggestion for this.
Global class BatchMassDeleteReports Implements Database.batchable<sobject>{
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator([SELECT Id from Report where CreatedDate <= LAST_N_DAYS:180 AND LastModifiedDate <= LAST_N_DAYS:180]);
}
global void execute(Database.BatchableContext BC,List<Report> scope){
list<Report> delReports = (list<Report>)scope;
if(delReports.CreatedDate <= LAST_N_DAYS:180 && delReports.LastModifiedDate <= LAST_N_DAYS:180)
{
delete delReports;
}
}
global void finish(Database.BatchableContext BC){
}
}
http://mysalesforcestuff.blogspot.com/2015/06/bulk-deleting-reports-best-way.html
Thanks
Ramesh
All Answers
No you doing it wrong you must use LastRunDate or LastViewedDate fields to identify reports which are unused in 6 months I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
Please find the modified batch class code below to delete the reports which 6 months old :
global class BatchMassDeleteReports implements Database.Batchable<sObject>{
public Database.QueryLocator start(Database.BatchableContext context)
{
return Database.getQueryLocator('Select ID from Report where LastRunDate <= LAST_N_DAYS:180');
}
public void execute(Database.BatchableContext context, List<Report> records)
{
delete records;
}
public void finish(Database.BatchableContext){
}
}
i am getting the error - Unexpected token ')'. at line 13 column 48
I am not sure where i am going wrong
Also find the schedule class below :
public DeleteReportSchedulerClass() {
}
global void execute(SchedulableContext sc){
try{
//Executing batch class (size:50).
BatchMassDeleteReports batchApexSchd = new BatchMassDeleteReports();
database.executebatch(batchApexSchd, 50);
}catch(exception ex){
//Catching any exception that occurs.
System.Debug('There was an error ' + ex.getMessage());
}
}
}
are there any changes required please do let me know.
Thanks,
Ramesh
You can't delete Reports in a bulk. Because Report Object doesn't allow any DML Operations.
It allows only the following operation,
describeSObjects(), query(), retrieve(), search()
There's a inbuild feature where you can mass delete reports
In Setup, under Data Management > Mass Delete Records, select Mass Delete Reports and configure a filter (for example based on Lastrun date) to find reports that need to be deleted.
Reports that you delete go into the recycle bin. They aren’t permanently deleted until you clear your recycle bin.
Thanks
Ramesh
Thank you for the valuable information. But will there be any possible way to automate the deletion of reports without going through the manual
process ?
Regards,
Ramesh
http://mysalesforcestuff.blogspot.com/2015/06/bulk-deleting-reports-best-way.html
Thanks
Ramesh