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
realtimetasks1.3941822547955107E12realtimetasks1.3941822547955107E12 

Trigger with Account Team Object

1) Enable account team member
follow navigation  setup==>customize==>account====>Account teams .
once you enable this accountteam member object is added to related list of account.

2) create one check box VPA on account object.

now the requirement is:


create one record in account (call parent record) mention VPA enable and don't select parent look up field.

create  one more record in account(call child) uncheck VPA and select (parent record) as parent for this child record using look up.

here VPA is enable means that record is parent other wise it's a child record...

Need to be achieve:

1) once you create a child record then owner of parent record should be add to child   related accountteam member.(parent owner should be insert as accounteam meber to child)

2) if you change the owner in parent it should replace the old owner with new onwer.
Ramu_SFDCRamu_SFDC
Hi,

Your 1st condition can be met with the following code. This is just a sample code for your idea and needs to improvise for actual use. Here -
** Parent_chkbox is the one that is like VPA in your case.

trigger Acctteammember_parentacct on Account (after insert, after update) {
  
    if(trigger.isinsert){
    for(account acct:trigger.new){
        if(acct.ParentId!=null && acct.Parent_Chkbox__c==true){
            Account Parentacct=[select id,ownerid from Account where id=:acct.ParentId limit 1];
           Accountteammember acctmem=new Accountteammember();
           acctmem.AccountId=acct.id;
           acctmem.UserId=Parentacct.OwnerId;
           acctmem.TeamMemberRole='Account Manager';           
            insert acctmem;
        }
    }
}
}

Regarding your 2nd point as the user id field of Account team members is not 'Updatable' as per SOAP Api definition for AccountTeamMember it is not possible to update the user id of it. The only way is to delete and create new account member with the changed owner by fething the field values from old account team member.
The sample code goes something as below. Once again this is just to give you an idea and it works fine for single record manipulations. This code needs to be improvised for bulk tasks.

if(trigger.isupdate){
        list<account> childacct=new list<account>();       
        for(account acct:trigger.new){
            account oldacct=trigger.oldmap.get(acct.Id);
            id oldparentownerid=oldacct.OwnerId;
            if(acct.ownerid!=oldparentownerid){
        if(acct.ParentId==null && acct.Parent_Chkbox__c==true){
            childacct=[select id from account where parentid=:acct.Id];
            for(account childaccts:childacct){              
                list<Accountteammember> acctteammem=new list<Accountteammember>();
                acctteammem=[select id,accountid,TeamMemberRole,UserId from accountteammember where accountid=:childaccts.Id and userid=:oldparentownerid];
                list<Accountteammember> childacctmem=new list<Accountteammember>();
                for(accountteammember mem:acctteammem){
                  AccountTeamMember newacctmem=new AccountTeamMember();
                    newacctmem.AccountId=mem.AccountId;
                    newacctmem.TeamMemberRole=mem.TeamMemberRole;
                    newacctmem.UserId=acct.ownerid;         
                  childacctmem.add(newacctmem);
                    delete mem;
               }               
              insert childacctmem;
            }
        }
       }   
    }
    }


Complete trigger Code

trigger Acctteammember_parentacct on Account (after insert, after update) {
   
    if(trigger.isinsert){
    for(account acct:trigger.new){
        if(acct.ParentId!=null && acct.Parent_Chkbox__c==true){
           Account Parentacct=[select id,ownerid from Account where id=:acct.ParentId limit 1];
           Accountteammember acctmem=new Accountteammember();
           acctmem.AccountId=acct.id;
           acctmem.UserId=Parentacct.OwnerId;
           acctmem.TeamMemberRole='Account Manager';            
           insert acctmem;
        }
    }
}
    if(trigger.isupdate){
        list<account> childacct=new list<account>();       
        for(account acct:trigger.new){
            account oldacct=trigger.oldmap.get(acct.Id);
            id oldparentownerid=oldacct.OwnerId;
            if(acct.ownerid!=oldparentownerid){
        if(acct.ParentId==null && acct.Parent_Chkbox__c==true){
            childacct=[select id from account where parentid=:acct.Id];
            for(account childaccts:childacct){              
                list<Accountteammember> acctteammem=new list<Accountteammember>();
                acctteammem=[select id,accountid,TeamMemberRole,UserId from accountteammember where accountid=:childaccts.Id and userid=:oldparentownerid];
                list<Accountteammember> childacctmem=new list<Accountteammember>();
                for(accountteammember mem:acctteammem){
                  AccountTeamMember newacctmem=new AccountTeamMember();
                    newacctmem.AccountId=mem.AccountId;
                    newacctmem.TeamMemberRole=mem.TeamMemberRole;
                    newacctmem.UserId=acct.ownerid;         
                  childacctmem.add(newacctmem);
                    delete mem;
               }               
              insert childacctmem;
            }
        }
       }   
    }
    }
}

Please mark this as best answer if this resolved your requirment.

Thanks,
Ramu
Anish Kumar 70Anish Kumar 70
I have used this code but got the  following error
Variable does not exist: AccountId
Variable does not exist: UserId
Variable does not exist: TeamMemberRole
DML requires SObject or SObject list type: AccountTeamMember