We are doing full org deployment, we need to update the record owner to system admin for all the objects records. So initially we created batch apex to implement this for accounts object. But we have around 400 objects and it is very difficult to run the batch class for each object. So we are thinking of using custom settings in order to run this batch class for all objects.
Could it be possible to create a custom settings and add all the 400 objects and process the custom settings in the batch class? If yes, could you please help us how to implement.
Below is the batch class for one object (account):
global class AccountOwnerUpdate implements Database.Batchable<sObject>
{
global Set<Id> userIds = new Set<Id>();
global AccountOwnerUpdate(){
List<String> userEmailList =new List<String>{'[email protected]','[email protected]'};
List<String> userNameList =new List<String>{'[email protected]','[email protected]'};
for(User objUser: [Select id from User Where (Email IN: userEmailList OR UserName IN:userNameList) ]){
userIds.add(objUser.id);
}
}
global Database.QueryLocator start(Database.BatchableContext bc) {
//String recId = '0014000000PcRkmAAF';
//String query ='Select id,OwnerId,LastModifiedDate from Account Where ID =:recId '; // change object name
String query ='Select id,OwnerId,LastModifiedDate from Account'; // change object name
//String query ='Select id,OwnerId from Account Limit 1 ';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<Account> records){
User objUser = [Select id,email from User Where email ='[email protected]' limit 1];
system.debug('objUser=='+objUser);
for(Account acc: records){//change object name
system.debug('acc.ownerId==='+acc.ownerId);
if(!userIds.contains(acc.ownerId)){
acc.ownerId = objUser.id;
//acc.LastModifiedDate = acc.LastModifiedDate;
}
}
system.debug('records=='+records);
update records;
}
global void finish(Database.BatchableContext bc){
// execute any post-processing operations
}
}