You need to sign in to do that
Don't have an account?
Ravi Nagar
Querying multiple objects in Batch Apex
My requirement was to look for all the records deleted by a particular user spanning mulitple objects and delete those records from recycle bin. I tried to come up with this code and it worked, but when there is too much of data it hits Apex governor limit. I am new to coding and don't know how to circumvent that. I initially tried writing my query in start method but I was not able to query multiple objects there using for loop.
I am using custom setting (EmptyRecycleBinBatch) to hold object API names.. Any suggestion is appreciated!
global class EmptyRecycleBinBatch implements Database.Batchable<sObject>, Schedulable {
global Database.QueryLocator start(Database.BatchableContext BC) {
String Query='Select Id, ObjectAPIName__c From EmptyRecycleBinBatch__c';
return database.getquerylocator(Query);
}
global void execute(Database.BatchableContext BC,List<EmptyRecycleBinBatch__c> sObjRecords) {
System.debug('in execute method');
User userid= [Select Id, Name from User where User.Name= ' test user' And isActive= true Limit 1];
String Query;
for(EmptyRecycleBinBatch__c sOb: sObjRecords){
SYstem.debug('object name :'+sOb.ObjectAPIName__c);
Query = 'SELECT ID from '+sOb.ObjectAPIName__c+' where isDeleted = true AND CreatedById = \''+userid.Id+'\' all rows';
system.debug('query '+Query );
List<Sobject> sObjtocreate = new List<Sobject>();
sObjtocreate = Database.query(Query);
System.debug('list of items to delete'+sObjtocreate);
if(sObjtocreate != null && !sObjtocreate.isEmpty()) {
try{
//system.debug(' deleting :'+sObjtocreate);
Database.emptyRecycleBin(sObjtocreate);
if(test.isRunningTest())
{
throw new applicationException('Exception');
}
}
catch(Exception e){
System.debug('ERROR on Delete:' + e);
}
}
}
}
public class applicationException extends Exception
{
}
global void finish(Database.BatchableContext BC) {
}
global void execute(SchedulableContext sc) {
database.executebatch(new EmptyRecycleBinBatch());
}
}
I am using custom setting (EmptyRecycleBinBatch) to hold object API names.. Any suggestion is appreciated!
global class EmptyRecycleBinBatch implements Database.Batchable<sObject>, Schedulable {
global Database.QueryLocator start(Database.BatchableContext BC) {
String Query='Select Id, ObjectAPIName__c From EmptyRecycleBinBatch__c';
return database.getquerylocator(Query);
}
global void execute(Database.BatchableContext BC,List<EmptyRecycleBinBatch__c> sObjRecords) {
System.debug('in execute method');
User userid= [Select Id, Name from User where User.Name= ' test user' And isActive= true Limit 1];
String Query;
for(EmptyRecycleBinBatch__c sOb: sObjRecords){
SYstem.debug('object name :'+sOb.ObjectAPIName__c);
Query = 'SELECT ID from '+sOb.ObjectAPIName__c+' where isDeleted = true AND CreatedById = \''+userid.Id+'\' all rows';
system.debug('query '+Query );
List<Sobject> sObjtocreate = new List<Sobject>();
sObjtocreate = Database.query(Query);
System.debug('list of items to delete'+sObjtocreate);
if(sObjtocreate != null && !sObjtocreate.isEmpty()) {
try{
//system.debug(' deleting :'+sObjtocreate);
Database.emptyRecycleBin(sObjtocreate);
if(test.isRunningTest())
{
throw new applicationException('Exception');
}
}
catch(Exception e){
System.debug('ERROR on Delete:' + e);
}
}
}
}
public class applicationException extends Exception
{
}
global void finish(Database.BatchableContext BC) {
}
global void execute(SchedulableContext sc) {
database.executebatch(new EmptyRecycleBinBatch());
}
}
Thanks
Shashikant
your flow in that case will be
- fetch ojects to query from custom settings
- loop over the API Names and run dynamic query one by one
- loop over records from query and add items to sobject list
ThanksShashikant
I have used your approach to full fill my req (Update case when an article is attached to it). But it seems to be trowing an error when casting the scope variable to casearticle (Since scope consists of both case records and casearticle records ). Could you please let me know how to differentiate between 2 object records before casting them to respective objects.
Batch Apex
Iterbale
I am getting similar error. Were you able to fix the error ?
Thanks!!
i need to implement same requirement as like your requirement... if it is working, can you plase your code ?