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
PappuPappu 

Trigger to add new Account team member

Hi Guys,

I have a custom object "Account_Team_Relationship__c" which is related to standard object Account.

Wheneever user is creating new Account_Team_Relationship__c object record, User details (Name, Account Access, Contact Access, Opportunity Access, Case Access and role) should be automaticaly saved to Account Team members, if the user details are not available in Account team members.

As of now Team role can be hardcoded.

Is it possible to do this with trigger?

Your thoughts are highly appreciated.
bhanu_prakashbhanu_prakash
Hi Papu,
Mark as best answer, If it resloves !!
upgrade these code based on your requirement
trigger TeamMemberTrg on Account(after insert) {
    if (trigger.isinsert) {
        set parentIds = new set();
        for (account acct: trigger.new) {
            if (acct.ParentId != null) {
                parentIds.add(acct.parentId);
            }
        }

        if (!parentIds.isEmpty()) {
            map mpAccs = new map([select id, ownerId from Account where Id in: parentIds]);
            List members = new List();
            for (account acct: trigger.new) {
                if (acct.ParentId != null && mpAccs.containsKey(acct.ParentId)) {
                    members.add(New AccountTeamMember(AccountId = acct.id, TeamMemberRole = ‘Account Manager’, UserId = mpAccs.get(acct.ParentId).OwnerId));
                }
            }

            if (!members.isEmpty())
                insert members;
        }

    }
}

Mark as resloved if it helps :) :)
Thanks, 
Bhanu Prakash
visit ForceLearn.com  (https://www.forcelearn.com)
PappuPappu
Thanks for the reply Bhanu.
I am a newbie to salesforce and not much familiar with triggers.
I tried with below code, but still facing an issue.

trigger AccountTeamChanges on Account_Team_Relationship__c (after insert) {
List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
List<AccountShare> acctSharingRules = new List<AccountShare>();
 
 for(Account_Team_Relationship__c a : Trigger.new)
 {
  if(Trigger.isInsert)
        {
            AccountTeamMember ca = new AccountTeamMember();
            ca.AccountId = a.Id;
            ca.TeamMemberRole = 'xxxxx';
            acctMembers.add(ca);
            
            AccountShare caSharingRule = new AccountShare();
            caSharingRule.AccountId = a.Id;
            caSharingRule.OpportunityAccessLevel = 'Read';
            caSharingRule.CaseAccessLevel = 'Read';
            caSharingRule.AccountAccessLevel = 'Edit';
            acctSharingRules.add(caSharingRule);
              
 }
 insert acctMembers;
insert acctSharingRules ;

 }}
 
bhanu_prakashbhanu_prakash
Hi,

Can you post your errors with line numbers
PappuPappu
Apex trigger AccountTeamChanges caused an unexpected exception, contact your administrator: AccountTeamChanges: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Account ID: id value of incorrect type: a5nZ0000000DZEqIAO: [AccountId]: Trigger.AccountTeamChanges: line 25, column 1
bhanu_prakashbhanu_prakash
trigger AccountTeamChanges on Account_Team_Relationship__c (after insert) {
List<AccountTeamMember> acctMembers = new List<AccountTeamMember>();
List<AccountShare> acctSharingRules = new List<AccountShare>();
 
 for(Account_Team_Relationship__c a : Trigger.new)
 {
  if(Trigger.isInsert)
        {
            AccountTeamMember ca = new AccountTeamMember();
            ca.AccountId = a.Id;
            ca.TeamMemberRole = 'xxxxx';
            acctMembers.add(ca);
             insert acctMembers;

            AccountShare caSharingRule = new AccountShare();
            caSharingRule.AccountId = a.Id;
            caSharingRule.OpportunityAccessLevel = 'Read';
            caSharingRule.CaseAccessLevel = 'Read';
            caSharingRule.AccountAccessLevel = 'Edit';
            acctSharingRules.add(caSharingRule);
             insert acctSharingRules ;
 } 
 }}

 
PappuPappu
Same Error.