I've tried both the AccountShare approach, and AccountAccessLevel approach, but neither has worked so far.
Below is the AccountShare attempt. Again the first part works in that it correctly creates the AccountTeamMember, but all the access levels are set to private.
public static void updateAccountTeamFromTopAcct(List<Target_Account__c> ttAcctList) {
List<Target_Account__c> ttUpdateList = new List<Target_Account__c>(); // trigger list
List<String> assignedUserIdsList = new List<String>(); // Ids from Assigned Top Account users
List<String> accountList = new List<String>(); // accounts IDs affected by Top Account
Map<String,String> roleMap = new Map<String,String>();
for(Target_Account__c ttAcc : ttAcctList){
assignedUserIdsList.add(ttAcc.Assigned_User__c);
accountList.add(ttAcc.Account__c);
roleMap.put(ttAcc.Assigned_User__c, ttAcc.User_Role__c);
}
List<Account> affectedAccounts = [select Id from Account where Id in : accountList]; // list of all accounts affected by top account
List<AccountTeamMember> accountTeamList = new List<AccountTeamMember>(); // list of AccountTeamMembers to insert
for(Account acct : affectedAccounts){
AccountTeamMember acctTM = new AccountTeamMember();
acctTM.AccountId = acct.Id;
for(String usrId: assignedUserIdsList){
acctTM.userId = usrId;
if(roleMap.get(usrId).equals('SpecialRole')){
acctTM.TeamMemberRole = 'Sales Rep';
accountTeamList.add(acctTM);
}
}
}
insert accountTeamList;
List<AccountShare> acctShareList = new List<AccountShare>();
List<AccountShare> affectedAcctShare = [select Id from AccountShare where AccountId in :affectedAccounts and UserOrGroupId in :assignedUserIdsList];
for (AccountShare share : affectedAcctShare ) {
// Give view access to opportunities and edit access to contacts
share.ContactAccessLevel = 'Edit';
share.OpportunityAccessLevel = 'Read';
acctShareList.add(share);
}
update acctShareList;affectedAcctShare;