function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lakshmi SLakshmi S 

After delete batch apex issue ?

Hi Team,

I am calling batch apex using trigger after deleting the record, but Start method returns null.
How to query deleted records in batch apex start method.
Trigger :
----------

if(Trigger.isDelete){
                
   Database.executeBatch(new BatchUpdateAcct(Trigger.OldMap),1);
               
}

Batch Class
-------------

global class BatchUpdateAcct implements Database.Batchable<sObject> {
    
    Map<Id,Object__c> mapRecs = new Map<Id,Object__c>();
    
    global BatchUpdateAcct(Map<Id,Object__c> oldValue){
        mapRecs = oldValue;
       
    }
    
    global Database.QueryLocator start (Database.BatchableContext BC){
        System.debug('----Start Method-----'+mapRecs.keySet());
        return Database.getQueryLocator([Select id,Name from Object__c where id in :mapRecs.KeySet()]);
    }
    
    global void execute (Database.BatchableContext BC, List<Object__c> objList){
        
    }
    
    global void finish(Database.BatchableContext BC){
        
    }

}

Please advise any one how to resolve this issue ?
Is it possible to call the batch apex after deleting the records ?


Thanks,
Lakshmi S
 
Best Answer chosen by Lakshmi S
Khan AnasKhan Anas (Salesforce Developers) 
Hi Lakshmi,

Greetings to you!

To get or Query deleted records you need to use ALL ROWS. So your query will be:
 
SELECT Id,Name FROM Object__c WHERE id in :mapRecs.KeySet() ALL ROWS

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Anant KamatAnant Kamat
Did you try printing the value of oldValue in debug logs to ascertain that you are actually getting values from the trigger.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Lakshmi,

Greetings to you!

To get or Query deleted records you need to use ALL ROWS. So your query will be:
 
SELECT Id,Name FROM Object__c WHERE id in :mapRecs.KeySet() ALL ROWS

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_query_all_rows.htm


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Lakshmi SLakshmi S
Hi Khan Anas,

Thanks for your reply.
I have requirement, can you please advise how can we achieve below requirement .
Objects : Account & CustomObject__c (No Relation ship for these two objects)
In Account we have a Division field (Data like North,South,East,West).
In "CustomObject__c" we have Name & division__c (multipicklist) fields.
Example : Suppose we have 5000 Accounts with North division. If i create a record in "CustomObject__c" with North division, we need to update name from custom object to all 5000 Accounts.
How can we achieve this requirement.

Please advise.

Thanks,
Lakshmi S.