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
sunny sengar 17sunny sengar 17 

Assign Account Owner trigger. Please help

If the AccountTeamMember is created or updated:
     If Account Type = Prospect

      Assign related  Account to the user who is set as Sales Manager on the Account Team 
      In case this user is blank or inactive, do not update the Account Owner 


trigger Updateaccountower on AccountTeamMember (after insert, after Update) {
    
    Set <ID> accids = new Set <ID> ();
    List<Account> AccountSales = new List<Account>();
    
    Map <Id,AccountTeamMember> accMap = new Map<Id,AccountTeamMember>(); 
    
    for ( AccountTeamMember a: Trigger.New) {
        accids.add(a.Accountid);
        accMap.put(a.Accountid,a);
    }
    
    List <Account> acclist = [SELECT id, Type from Account where id in:accids];
    List <AccountTeamMember> ATMs = [SELECT AccountId, TeamMemberRole, UserId, AccountAccessLevel FROM AccountTeamMember WHERE AccountId in: accids AND TeamMemberRole ='Sales Representative'];
    system.debug('ATMs'+ATMs);
    
    for (AccountTeamMember AT : ATMs)
    {
        for(Account acclst : acclist) {
            system.debug('@@@@@@');
            if(acclst.Type =='Prospect') {
                Account acc = new Account();
                acc.id = AT.AccountId;
                acc.OwnerId = AT.UserId;
                AccountSales.add(acc);
            }
        }
    }
    update AccountSales;