You need to sign in to do that
Don't have an account?
Sapana W
change inactive owners to active owners for records
Hi All,
I am trying to update the owners of contact records to owner of parent account record. But for records where contact owner is inactive I am unable to change its owner. I have written a batch class but it does not work. Could anyone let me know whats the issue?(I have all the required permission to update the inactive owner records)
Here is my code:
global class BatchUpdateCon implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, owner.Isactive, OwnerId, Account.OwnerId from Contact where owner.isactive = False';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<Contact> scope)
{
List <Contact> contactToUpdate = new list<Contact>();
Set<Id> accIds = new Set<Id>();
for(Contact con : scope)
{
accIds.add(con.AccountId);
}
Map<Id, Account> mapAccount = new Map<Id, Account>([select id, ownerId from Account where id in:accIds]);
for(Contact con : scope)
{
if(con.AccountId != null && con.OwnerId != mapAccount.get(con.AccountId).OwnerId)
{
System.debug('Inside--->'+con.Accountid);
con.OwnerId = mapAccount.get(con.AccountId).OwnerId;
contactToUpdate.add(con);
}
}
update contactToUpdate;
}
global void finish(Database.BatchableContext BC)
{
}
}
I am trying to update the owners of contact records to owner of parent account record. But for records where contact owner is inactive I am unable to change its owner. I have written a batch class but it does not work. Could anyone let me know whats the issue?(I have all the required permission to update the inactive owner records)
Here is my code:
global class BatchUpdateCon implements Database.Batchable<sObject>
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'Select Id, owner.Isactive, OwnerId, Account.OwnerId from Contact where owner.isactive = False';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC,List<Contact> scope)
{
List <Contact> contactToUpdate = new list<Contact>();
Set<Id> accIds = new Set<Id>();
for(Contact con : scope)
{
accIds.add(con.AccountId);
}
Map<Id, Account> mapAccount = new Map<Id, Account>([select id, ownerId from Account where id in:accIds]);
for(Contact con : scope)
{
if(con.AccountId != null && con.OwnerId != mapAccount.get(con.AccountId).OwnerId)
{
System.debug('Inside--->'+con.Accountid);
con.OwnerId = mapAccount.get(con.AccountId).OwnerId;
contactToUpdate.add(con);
}
}
update contactToUpdate;
}
global void finish(Database.BatchableContext BC)
{
}
}