Skip to main content
deleted 92 characters in body
Source Link

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;

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;

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> 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;
Source Link

Add user to Account Team and set Access Levels

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.

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.

Thanks!

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;