+ Start a Discussion
Deepak Pandey 13Deepak Pandey 13 

trigger to update cutom field user insert accountteammember

trigger to insert and update cutom field user on account . 
1- insert time hard coded value fill in accountteammember.
2-update time user (look up of user) change than accountteammember user will be same.

trigger-

trigger atm on account(after insert,after update)
{

  list<AccountTeamMember> accountTeamList=new list<AccountTeamMember>();
 
  
   if(Trigger.isInsert)
     for(account a:trigger.new)
     {
    
        AccountTeamMember acctTM = new AccountTeamMember();
                    AccountShare acctShare = new AccountShare();
                    acctTM.AccountId = a.id;
                    acctTM.userId = a.user__c;
                    acctTM.TeamMemberRole = 'Account Manager';
                    accountTeamList.add(acctTM);   
   }
       for(account acc:trigger.new)
    {
     Account oldAccount = Trigger.oldMap.get(acc.ID);
 if(acc.user__c != oldAccount.user__c)
         
        {       
                AccountTeamMember acctTM = new AccountTeamMember();
                    AccountShare acctShare = new AccountShare();
                    acctTM.AccountId = acc.id;
                    acctTM.userId = acc.user__c;
                    //acctTM.TeamMemberRole = 'Account Manager';
                    accountTeamList.add(acctTM);   
        } 
     
     }       
           
insert accountTeamList;
}
ManojjenaManojjena
Hi Deepak,
Can you explain your requirment clearly ? So that we can help you .
Deepak Pandey 13Deepak Pandey 13
account have a lookup of user. whn account insert than accountteammember which is child of account will created. after account we change the user field of existing record accountteammember record have a userid wil also updated and will be same
.
ManojjenaManojjena
HI Deepak,

Try with below code it wil help !!
trigger atm on account(after insert,after update){
   list<AccountTeamMember> accountTeamList=new list<AccountTeamMember>();
   AccountTeamMember acctTM = new AccountTeamMember();
    if(Trigger.isAfter){
        if(Trigger.isInsert){
			for(Account acc: Trigger.new){
				acctTM.AccountId = a.id;
				acctTM.userId = a.user__c;
				acctTM.TeamMemberRole = 'Account Manager';
				accountTeamList.add(acctTM);
			}
	    }if(Trigger.isUpdate){
			Map<Id,Id> acccIdWithUserIdMap=new Map<Id,Id>();
			for(Account acc: Trigger.new){
				if(Trigger.oldMap.get(acc.Id)user__c != acc.User__c && acc.User__c != null ){
					acccIdWithUserIdMap.put(acc.Id,acc.User__c);
				}
			}
			if(!acccIdWithUserIdMap.isEmpty()){
			  List<AccountTeamMember> accTmLst=[SELECT AccountId,userId,TeamMemberRole FROM AccountTeamMember WHERE AccountId IN :acccIdWithUserIdMap.keySet() ];
			  if(accTmLst.size() > 0){
			     for(AccountTeamMember atmm : accTmLst){
				   atmm.userId=acccIdWithUserIdMap.get(atmm.AccountId);
				   accountTeamList.add(atmm);
				 }
			  }
			}
        }
	}
	if(accountTeamList.isEmpty()){
	   try{
	      upsert accountTeamList;
	   }catch(DmlException de){
	      System.debug(de);
	   }
	}
}

Let me know if it helps !!!

Thanks
Mnaoj
Deepak Pandey 13Deepak Pandey 13
@manoj     Trigger.atm: line 28, column 1...System.SObjectException: Field is not writeable.
ManojjenaManojjena
Hi Deepak ,

Nothing is there in line 28 ,is is it possible to share your debug log ?
 
Deepak Pandey 13Deepak Pandey 13
User-added image
ManojjenaManojjena
Hi Deepak ,

Account teamMember userId field is is readonly .So you can try like below .
 
trigger atm on account(after insert,after update){
   list<AccountTeamMember> accountTeamList=new list<AccountTeamMember>();
   AccountTeamMember acctTM = new AccountTeamMember();
    if(Trigger.isAfter){
        if(Trigger.isInsert){
			for(Account acc: Trigger.new){
				acctTM.AccountId = a.id;
				acctTM.userId = a.user__c;
				acctTM.TeamMemberRole = 'Account Manager';
				accountTeamList.add(acctTM);
			}
	    }if(Trigger.isUpdate){
			Map<Id,Id> acccIdWithUserIdMap=new Map<Id,Id>();
			for(Account acc: Trigger.new){
				if(Trigger.oldMap.get(acc.Id)user__c != acc.User__c && acc.User__c != null ){
					acccIdWithUserIdMap.put(acc.Id,acc.User__c);
				}
			}
			if(!acccIdWithUserIdMap.isEmpty()){
			  List<AccountTeamMember> accTmLst=[SELECT AccountId,userId,TeamMemberRole FROM AccountTeamMember WHERE AccountId IN :acccIdWithUserIdMap.keySet() ];
			  if(accTmLst.size() > 0){
			     for(AccountTeamMember atmm : accTmLst){
					acctTM.AccountId = a.id;
					acctTM.userId = acccIdWithUserIdMap.get(atmm.AccountId);
					acctTM.TeamMemberRole = 'Account Manager';
					accountTeamList.add(acctTM);
				    accountTeamList.add(atmm);
				 }
			  }
			}
        }
	}
	if(accountTeamList.isEmpty()){
	   try{
	      insert accountTeamList;
	   }catch(DmlException de){
	      System.debug(de);
	   }
	}
	 try{
		delete accTmLst;
	}catch(DmlException ex){
	      System.debug(ex);
	}
}

Let me know if it works or still issue .

Thanks 
Manoj
 
ManojjenaManojjena
Deepak ,
Check with your team ,whether you want to delete the previous TeamMember of you want to keep it .
Deepak Pandey 13Deepak Pandey 13

        delete accTmLst;
    }catch(DmlException ex){
         System.debug(ex);
 }...not working

 accountteammember not ceated !
ManojjenaManojjena
Check the debug log ,
Deepak Pandey 13Deepak Pandey 13
User-added image