You need to sign in to do that
Don't have an account?
Joshua Stein 4
Custom Field is Being Updated via Apex Class
I'm an admin that doesn't know apex and I'm troubleshooting a custom field on the Cases object that's getting blanked out. SFDC support told me which apex class is doing it, apparently because they see field update depencies associated with it which list the custom field in question.
Since I'm not a developer, and the developer that wrote this isn't with the company, I'm having a hard time figuring out why it's doing this. Can anyone tell me what this apex class is doing?
global class CaseTeamMembersCleanupBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id,Name, OwnerId FROM Account';
if(Test.isRunningTest())
query = 'SELECT Id, Name, OwnerId FROM Account order by Id Desc limit 200';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope){
Map<Id, Id> AccountIds = new Map<Id, Id>();
Map<Id, List<AccountHistory>> accountHistoryMap;
Map<Id, Set<Id>> oldOwnerMap;
List<CaseTeamMember> teamMembersForDelete = new List<CaseTeamMember>();
//List<Account
//get account Ids in this context
for(Account act: scope){
AccountIds.put(act.Id, act.OwnerId);
}
accountHistoryMap = new Map<Id, List<AccountHistory>>();
oldOwnerMap = new Map<Id, Set<Id>>();
String oldValue;
List<AccountHistory> accountHistoryLst = [Select Id, OldValue, NewValue, Field, AccountId from AccountHistory
where field = 'Owner' and AccountId In: AccountIds.KeySet()];
if(Test.isRunningTest())
accountHistoryLst = new List<AccountHistory>{ new AccountHistory(field='Owner', AccountId=scope[0].Id)};
for(AccountHistory hist: accountHistoryLst)
{
oldValue = (Test.isRunningTest()) ? System.UserInfo.getUserId() : String.ValueOf(hist.OldValue);
if(System.Pattern.matches('[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}', oldValue)){
if(oldOwnerMap.containsKey(hist.AccountId)){
//accountHistoryMap.get(hist.AccountId).add(hist);
if( ! oldOwnerMap.get(hist.AccountId).Contains(Id.ValueOf(oldValue))) {
if(Id.ValueOf(oldValue) != AccountIds.get(hist.AccountId) ){
oldOwnerMap.get(hist.AccountId).add(Id.ValueOf(oldValue));
}
}
}
else
{
//accountHistoryMap.put(hist.AccountId, new List<AccountHistory>{hist});
oldOwnerMap.put(hist.AccountId, new Set<Id> {Id.ValueOf(oldValue)}) ;
}
}
}
//Get all the cases from account with team members
Set<Id> AccountOldValues = new Set<Id>();
for(Case csc: [Select Id, AccountId, (Select Id, MemberId from TeamMembers )
from Case where AccountId IN: oldOwnerMap.KeySet()])
{
AccountOldValues = oldOwnerMap.get(csc.AccountId);
for(CaseTeamMember member: csc.TeamMembers){
//check if this member is part of old value from history table
if(AccountOldValues.contains(member.MemberId)){
teamMembersForDelete.add(member);
}
}
}
if(teamMembersForDelete.size()!=0)
delete teamMembersForDelete;
}
global void finish(Database.BatchableContext BC){
}
}
Since I'm not a developer, and the developer that wrote this isn't with the company, I'm having a hard time figuring out why it's doing this. Can anyone tell me what this apex class is doing?
global class CaseTeamMembersCleanupBatch implements Database.Batchable<sObject>{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id,Name, OwnerId FROM Account';
if(Test.isRunningTest())
query = 'SELECT Id, Name, OwnerId FROM Account order by Id Desc limit 200';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope){
Map<Id, Id> AccountIds = new Map<Id, Id>();
Map<Id, List<AccountHistory>> accountHistoryMap;
Map<Id, Set<Id>> oldOwnerMap;
List<CaseTeamMember> teamMembersForDelete = new List<CaseTeamMember>();
//List<Account
//get account Ids in this context
for(Account act: scope){
AccountIds.put(act.Id, act.OwnerId);
}
accountHistoryMap = new Map<Id, List<AccountHistory>>();
oldOwnerMap = new Map<Id, Set<Id>>();
String oldValue;
List<AccountHistory> accountHistoryLst = [Select Id, OldValue, NewValue, Field, AccountId from AccountHistory
where field = 'Owner' and AccountId In: AccountIds.KeySet()];
if(Test.isRunningTest())
accountHistoryLst = new List<AccountHistory>{ new AccountHistory(field='Owner', AccountId=scope[0].Id)};
for(AccountHistory hist: accountHistoryLst)
{
oldValue = (Test.isRunningTest()) ? System.UserInfo.getUserId() : String.ValueOf(hist.OldValue);
if(System.Pattern.matches('[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}', oldValue)){
if(oldOwnerMap.containsKey(hist.AccountId)){
//accountHistoryMap.get(hist.AccountId).add(hist);
if( ! oldOwnerMap.get(hist.AccountId).Contains(Id.ValueOf(oldValue))) {
if(Id.ValueOf(oldValue) != AccountIds.get(hist.AccountId) ){
oldOwnerMap.get(hist.AccountId).add(Id.ValueOf(oldValue));
}
}
}
else
{
//accountHistoryMap.put(hist.AccountId, new List<AccountHistory>{hist});
oldOwnerMap.put(hist.AccountId, new Set<Id> {Id.ValueOf(oldValue)}) ;
}
}
}
//Get all the cases from account with team members
Set<Id> AccountOldValues = new Set<Id>();
for(Case csc: [Select Id, AccountId, (Select Id, MemberId from TeamMembers )
from Case where AccountId IN: oldOwnerMap.KeySet()])
{
AccountOldValues = oldOwnerMap.get(csc.AccountId);
for(CaseTeamMember member: csc.TeamMembers){
//check if this member is part of old value from history table
if(AccountOldValues.contains(member.MemberId)){
teamMembersForDelete.add(member);
}
}
}
if(teamMembersForDelete.size()!=0)
delete teamMembersForDelete;
}
global void finish(Database.BatchableContext BC){
}
}
Abdul Khatri
Looking at the quick glance I guess this batch is removing the old Team Members from the Account Team, when a new Team Member is added.