I have a requirement that based on the account owner and country ,account team has to be
automatically assigned .what are ways we can do it?
make a comment if you dont understand the question.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
trigger AccountTeam on Account (after update) { 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 Account a1 = [select id, parent.Id,Owner from account Where Id=:trigger.new.ParentID]; ID uid = a1.Ownerid; //get the user id of the user running the trigger, anyone that changes the Account will added to the account team for(Account a:trigger.new) { AccountTeamMember Teammemberad=new AccountTeamMember(); Teammemberad.AccountId=a.id; Teammemberad.UserId=uid; Teammemberad.TeamMemberRole = 'Account Modifier'; newmembers.add(Teammemberad); } Database.SaveResult[] lsr = Database.insert(newmembers,false); //insert any valid members then add their share entry if they were successfully added Integer newcnt=0; for(Database.SaveResult sr:lsr) { if(!sr.isSuccess()) { Database.Error emsg =sr.getErrors()[0]; system.debug('nnERROR ADDING TEAM MEMBER:'+emsg); } else { newShare.add(new AccountShare(UserOrGroupId=newmembers[newcnt].UserId, AccountId=newmembers[newcnt].Accountid, AccountAccessLevel='Read',OpportunityAccessLevel='Read')); } newcnt++; } Database.SaveResult[] lsr0 =Database.insert(newShare,false); //insert the new shares Integer newcnt0=0; for(Database.SaveResult sr0:lsr0) { if(!sr0.isSuccess()) { Database.Error emsg0 = sr0.getErrors()[0]; system.debug('nnERROR ADDING SHARING:'+newShare[newcnt0]+'::'+emsg0); } newcnt0++; } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0