I have a trigger that creates an AccountTeamMember object every time there is an update to an Account_Ownership__c object (this is a custom object that we've created in house to act more in line with what we needed).
(More Specifically, every time there is an update to an Account_Ownership__c object, my code deletes all AccountTeamMembers(and AccountShares), gets a list of all Account_Ownership__Cs on the related Account, and creates a new AccountTeamMember for each one)
As far as we can tell, when we use data loader to update 50,000 records in batches of 200, only one record per batch is being updated. I think my code should be able to handle bulk updates, so I was looking for some insight as to what may be causing these results. code below
Trigger:
trigger AccountOwnershipTrigger on Account_Ownership__c (before insert, before update, after insert, after update, after delete) {
...
...
if(Trigger.isAfter){
if(Trigger.isUpdate){
AccountOwnershipTriggerHelper.updateAccountTeamUsers(Trigger.new, Trigger.oldMap);
}
Code:
public static void updateAccountTeamUsers(List<Account_Ownership__c> newAcctOwnership, Map<Id, Account_Ownership__c> oldAcctOwnership){
// Gets a list of current Account Team Members
List<AccountTeamMember> acctTeamMembers = getAccountTeams(oldAcctOwnership);
List<AccountTeamMember> currentTeamMembers = new List<AccountTeamMember>();
List<AccountShare> currentAccountShares = new List<AccountShare>();
String acctID = '';
String backupAcctID = '';
// If there is at least one current Account Team Member
if (acctTeamMembers.size() > 0){
// Gets the AccountId of the first Team Member
acctID = acctTeamMembers[0].AccountID;
// Gets a list of all current Account Team Members
currentTeamMembers = [select id, userid from AccountTeamMember where accountID =:acctTeamMembers[0].AccountId];
currentAccountShares = [select id from AccountShare where accountID =: acctId AND RowCause = 'Manual'];
// If there is at least one current Team Member,
// Delete all Account Team Members
if(currentTeamMembers.size() > 0){
for(AccountTeamMember atm: currentTeamMembers){
AccountTeamMember tempTeamMember = new AccountTeamMember();
tempTeamMember = atm;
delete tempTeamMember;
}
}
if(currentAccountShares.size()>0){
for(AccountShare acctShare: currentAccountShares){
AccountShare tempShare = new AccountShare();
tempShare = acctShare;
delete tempshare;
}
}
List<Account_Ownership__c> uniqueAccountOwnerships = new List<Account_Ownership__c>();
uniqueAccountOwnerships = [select id, user__c, account__c from Account_Ownership__c where Salesforce_Account_ID__C =:acctId];
addUserToAccountTeam(uniqueAccountOwnerships);
}
// If there are no Team Members, but there is at least one new Account Ownership Being added
else if(newAcctOwnership.size() > 0)
{
try{
// Gets the AccountId of the first new Account Ownership
backupAcctID = newAcctOwnership[0].Account__c;
List<Account_Ownership__c> backupUniqueAccountOwnerships = new List<Account_Ownership__c>();
// Selects all Account Ownerships from the current Account
backupUniqueAccountOwnerships = [select id, user__c, account__c from Account_Ownership__c where Salesforce_Account_ID__C =:backupAcctId];
// Adds all Account Ownerships to Account Team Members
addUserToAccountTeam(backupUniqueAccountOwnerships);
}
catch (Exception e){
System.debug('You cant DO that');
}
}
Also: I know this is a wildy ineffiecient way to do what I wan't to do, but we were super hard pressed for time and needed to get a solution out ASAP (Please don't judge)