• Sofiya Sane
  • 0 Points
  • Member since 2016

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 1
I have custom object named 'Top Account' that is related to the standard Account object.

When creating new 'Top Accounts', it requires a user to be specified and a role. When saved, I want that user to be added to the Account Team, and give specific Access Levels (i.e. edit access to contacts, and read access to opportunities).

Adding the user to the Account Team is straightforward, but setting the Access Levels has proven to be pretty complex.

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){

    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;
                acctTM.TeamMemberRole = 'Sales Rep';

    insert accountTeamList;

    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';

    update affectedAcctShare;