+ Start a Discussion
Rajasree JayarajRajasree Jayaraj 

Account share edit access not getting granted via apex

Hi There,

I am trying to add a user called Account Manager (custom lookup field to User under Account) under the Account Team with Edit permission to Account, Opportunity, Contact. I tried to automate this via apex trigger on Account.

Here's my method called on Update:

public void insertAccTeamShare(List<account>newObjects,List<Account>oldObjects,Map<id,account>newMap,Map<id,account>oldMap)
  {
      List<AccountTeamMember> newmembersList = new List<AccountTeamMember>(); //list of new team members to add 
      List<AccountShare> newShareList = new List<AccountShare>(); //list of new shares to add 
      String accountAccessLevel = 'Edit', opportunityAccessLevel = 'Edit', caseAccessLevel = 'Edit',ContactAccessLevel='Edit';
      List<AccountTeamMember> deleteMemberList = new List<AccountTeamMember>();
      List<AccountShare>deleteShareList = new List<AccountShare>();
      Set<Id>updatedAccId = new set<ID>();
    for(account nAcc:newObjects)
    {
          if(nAcc.Account_Manager__c!=null)
                {
                 system.debug('AM='+nAcc.Account_Manager__c);
                   AccountShare accShare = new AccountShare();
                   accShare.AccountId=nAcc.id;
                   accShare.UserOrGroupId=nAcc.Account_Manager__c;
                   accShare.AccountAccessLevel=accountAccessLevel;
                   accShare.OpportunityAccessLevel=opportunityAccessLevel;
                   accShare.CaseAccessLevel = caseAccessLevel;
                   accShare.ContactAccessLevel = ContactAccessLevel;
                   newShareList.add(accShare);  
            
                   AccountTeamMember Teammemberad=new AccountTeamMember(); 
                   Teammemberad.AccountId=nAcc.id;
                   Teammemberad.UserId=nAcc.Account_Manager__c;
                   Teammemberad.TeamMemberRole = 'Account Manager'; 
                   newmembersList.add(Teammemberad); 
                   updatedAccId.add(nAcc.id);
               }  
            else{
                 updatedAccId.add(nAcc.id);
            }
           } 
        if(updatedAccId.size()>0)
        {
        deleteMemberList = [select id,AccountId from AccountTeamMember where AccountId=:updatedAccId];
        }
    List<AccountTeamMember>deleteMember = new List<AccountTeamMember>();

    if(deleteMemberList.size()>0)
    { 
     for(AccountTeamMember delMember: deleteMemberList)
     {  
         deleteMember.add(delMember);
     }
    }

    if(deleteMember.size()>0)
    {   try{
         delete deleteMember;
         }
       catch(Exception e){
          e.getmessage();
          system.debug('Exception HEREEEE');
      }    
    }

    if(newmembersList.size()>0 && newShareList.size()>0)
    {
       try{
          insert newmembersList;
          insert newShareList;
          }
      catch(Exception e){
          e.getmessage();
          system.debug('Exception HEREEEE');
      }
    }
  }


In the debug log I can see that Account Team member is getting inserted with Edit access, but when I check from the account, I see that the member got inserted with Read-Only & Private access.

Not sure if the record is getting rolled back and if so what could be the reason.

Any help here would be appreciated.

Thanks a lot!