Print this page

Add a user as an account team member in all the accounts owned by another user

Knowledge Article Number 000001097
Description

When you need to give a user access to all of the accounts owned by another user there are 2 solutions. Create a sharing rule, or using an anonymous block, that can be executed in the API, Force.com IDE or System Log window, to perform this task. 

 

Resolution

Before you get started - This solution requires developer skills, if you're not comfortable implementing Apex code we suggest that you use sharing rules as your solution.

Use an anonymous block, that can be executed in the API, Force.com IDE or in the System Log window,  to carry out this task. In the UI, this can be done as follows:

1. Click on System log across the top of the window.
2. Paste the code below in the Execute Apex pane.
 

ID userAID = [select id from user where name='John Smith'].ID;
ID userBID = [select id from user where name='Andrew Lee'].ID;
String teamMemberRole = 'Account Manager';
String accountAccessLevel = 'Edit', opportunityAccessLevel = 'Edit', caseAccessLevel = 'None';
List<Account> accounts = [select id from account where ownerid = :userAID];
List<AccountTeamMember> members = new list<AccountTeamMember>();
List<AccountShare> sharingRules = new list<AccountShare>();

for(Account a : accounts) {
    members.add(New AccountTeamMember(AccountId = a.id, TeamMemberRole = teamMemberRole, UserId= userBID));
    sharingRules.add(new AccountShare(AccountAccessLevel = accountAccessLevel, OpportunityAccessLevel = OpportunityAccessLevel, CaseAccessLevel = caseAccessLevel, UserOrGroupId = userBID, AccountId = a.id));
}

insert members;
insert sharingRules;
 

3. Replace 'John Smith' on line 1 with the full name of the user who owns all the accounts that are going to be shared.
4. Replace 'Andrew Lee' on line 2 with the full name of the user to whom we will share those accounts.
5. Make sure teamMemberRole on line 3 contains a valid account team member role.

  • Click Setup | Customize | Accounts | Account Teams | Team Roles

6. Specify the level of access to accounts, opportunities and cases on line 4. I.e. set accountAccessLevel, opportunityAccessLevel and caseAccessLevel to either 'Edit' (Read/Write), 'Read' (Read Only) or 'None' (Private).
7. Click Execute.
8. A new entry should show up in the Logs pane.

 

Considerations for using an anonymous block

 
  • Before executing this on a production org, please test it on a sandbox or test org, as this code will modify your production data.
  • As this will insert sharing rules, this could potentially cause contention on big orgs, so bear that in mind and if possible execute off peak.
  • This code will not allow to share more than 10k records, which is the limit imposed by the SOQL query.
  • If user B was already included in the account team of accounts owned by user A, his team role and level of accessibility will be overwritten.




promote demote