• Ravi Nagar
  • NEWBIE
  • 15 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
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 did this code but developer console says "expecting right curly bracket, found catch". I don't find any missing curly bracket in my code. Please help!

​public class AccountHandler {
    
    public static Account insertNewAccount(String AccountName){
        try{  Account acct= new Account(Name=AccountName) ;
        insert acct;
        return acct;
           }
    }
    catch(DmlException e){
        System.debug('A DML exception has occurred: ' +e.getMessage());
        return(NULL)
    }
   
}
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 did this code but developer console says "expecting right curly bracket, found catch". I don't find any missing curly bracket in my code. Please help!

​public class AccountHandler {
    
    public static Account insertNewAccount(String AccountName){
        try{  Account acct= new Account(Name=AccountName) ;
        insert acct;
        return acct;
           }
    }
    catch(DmlException e){
        System.debug('A DML exception has occurred: ' +e.getMessage());
        return(NULL)
    }
   
}
Hi All,
I am a learner in this platform. I was waondering is there any meterial which can help me to understand the use of Standard objects in slaesforce.

If you know any such meterial please suggest me the same.

Thanks in advance.