function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Che SFDCChe SFDC 

Help on AccountTeamMember trigger

All, below is my trigger which increases sharing access when a new Account Team Member is entered on an account. Trigger works fine but this trigger does not run when new Account Team Member is created. It only works when account is updated (which I understand because I have created a trigger on Account object). From what I understand, triggers cannot be created on Account Team Member. Is there a workaround?
 
trigger teamsharing on Account (before update) {
    
          List<AccountShare> shares = new List<AccountShare>();
        Set <ID> accids = New Set <ID> ();
         For ( Account A: Trigger.New) {
            accids.add(A.id);
    }

       List <AccountTeamMember> ATMs = [SELECT AccountId, TeamMemberRole, UserId, AccountAccessLevel FROM AccountTeamMember WHERE AccountId in: accids];
    
    For (AccountTeamMember AT : ATMs ) {

    
                     AccountShare aShare = new AccountShare();
          aShare.AccountId              = AT.AccountId;
          aShare.UserOrGroupId          = AT.UserId;
          aShare.AccountAccessLevel     = 'Edit';
          aShare.ContactAccessLevel     = 'Edit';
          aShare.OpportunityAccessLevel = 'Edit';
          shares.add(aShare);
    
        
    }
    Insert Shares;
}

 
Neetu_BansalNeetu_Bansal
Hi,

You can write trigger on Account Team Member instead of Account. Try the below one:
trigger teamsharing on AccountTeamMember( after insert, after update )
{
    List<AccountShare> shares = new List<AccountShare>();
    
	Map<Id, AccountTeamMember> accMap = new Map<Id, AccountTeamMember>();
	for( AccountTeamMember A : Trigger.New )
	{
		if( trigger.isInsert || ( trigger.isUpdate && A.AccountId != trigger.oldMap.get( A.Id ).AccountId ))
			accMap.put( A.AccountId, A );
    }

    for( Id i : accMap.KeySet())
	{
		AccountShare aShare = new AccountShare();
        aShare.AccountId = i;
        aShare.UserOrGroupId = accMap.get( i ).UserId;
        aShare.AccountAccessLevel = 'Edit';
        aShare.ContactAccessLevel = 'Edit';
        aShare.OpportunityAccessLevel = 'Edit';
        
		shares.add(aShare);
    }
	
	if( Shares.size() > 0 )
		Insert Shares;
}
Let me know, if you need any other help.

Thanks,
Neetu
ManojjenaManojjena
Hi Che,
I don,t think we  can write trigger in Account Team Member  .
Check with below code it may help !!
https://developer.salesforce.com/forums/?id=906F000000093bdIAA
CSECSE
I realize this is an old post but wanted to share that a solution is Now Available on the AppExchange! - Custom Account Team Management System (CATMS) (https://appexchange.salesforce.com/appxListingDetail?listingId=a0N3A00000FHBSKUA5) - The CATMS app allows you to build triggers and other automation that will execute when Account Team Member Records are Created, Edited or Deleted, while maintaining and enforcing access security and standerd functionality for Account Teams.