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

Upadte the exsiting records for Account team member

I'm trying to add owner of parent account under team member for child Account.Here is senario. Account P with VPA = true and Owner = PO.

· Account A with VPA = false (or true, doesn’t matter) and Owner = AO.

For Account A, I specify account P as parent account. Then, I expect the trigger to add PO (parent account owner) to the account team of Account A. And again, this should happen only if the parent account has VPA = true.

Here is my work ,which works while creating a new record. I need to extend this by including the update condition i.e if we change the parent owner correspondingly the teAM member has to be changed. trigger AccountTeam on Account (after insert,before update, after update) { if(trigger.isafter && Trigger.Isinsert ){

  for(Account acct :{

             Integer newcnt=0;
             Integer newcnt0=0;
             AccountTeamMember[] newmembers = new AccountTeamMember[]{};
              //list of new team members to add
              AccountShare[] newShare = new AccountShare[]{};
               //list of new shares to add

               list<Account> a1 = [select id,VPA__c, parent.Id,OwnerId from account Where Id=:acct.ParentID];
             for(account acc: a1){
               ID uid =  acc.OwnerId;
                Boolean x=acc.VPA__c;

               //get the user id of the parent Account, if VPA is checked and  will added to the account team
          if( a1.size()>0 && x == True)  {// checking size of list.
               for(Account {          
                        AccountTeamMember Teammemberad=new AccountTeamMember();
                         Teammemberad.TeamMemberRole = 'Global KA Manager';
                    Database.SaveResult[] lsr = Database.insert(newmembers,false);
                 //insert any valid members then add their share entry if they were successfully added ;
                 for(Database.SaveResult sr:lsr)
                 Database.Error emsg =sr.getErrors()[0];

                  system.debug('\n\nERROR ADDING TEAM MEMBER:'+emsg);
                   newShare.add(new AccountShare(UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Read',OpportunityAccessLevel='Read'));
                    Database.SaveResult[] lsr0 =Database.insert(newShare,false);
                     //insert the new shares Integer newcnt0=0;
                     for(Database.SaveResult sr0:lsr0)
                     Database.Error emsg0=sr0.getErrors()[0];
                     system.debug('\n\nERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0); } newcnt0++; }


Please review my reply to a post on similar requirment at the link

Hope that helps !!