+ Start a Discussion
Bill NicholsonBill Nicholson 

Can someone help me write a trigger "Use case, On Account object, when owner is edited and record type is household, add updated owner to account team.

Hemant_SoniHemant_Soni
Hi Bill,

Please Try Below Code:
trigger Account on Account (after update) {
	list<AccountTeamMember>lstTeamMember = new list<AccountTeamMember>(); 
    if(trigger.IsAfter){
        system.debug('@developer-->IsAfter');
        if(trigger.IsUpdate){
            system.debug('@developer-->IsUpdate');
            
        RecordType rtAccount = [Select Id,Name,developerName from Recordtype where sObjecttype='Account' And developerName='HouseHold'];
        
        for(Account oAccount:trigger.new){ 
            system.debug('@developoer-->oAccount:'+oAccount);
            system.debug('@developoer-->oAccount.RecordtypeId:'+oAccount.RecordtypeId);
            system.debug('@developoer-->oAccount.OwnerId:'+oAccount.OwnerId);
            system.debug('@developoer-->oAccount.trigger.oldmap.get(oAccount.Id).ownerId:'+trigger.oldmap.get(oAccount.Id).ownerId);
            
            if(oAccount.RecordtypeId == rtAccount.Id && oAccount.OwnerId != trigger.oldmap.get(oAccount.Id).ownerId){
            	
            	AccountTeamMember oATM = new AccountTeamMember ();
            	oATM.AccountId = oAccount.Id;
            	oATM.UserId = trigger.oldmap.get(oAccount.Id).ownerId;
            	oATM.TeamMemberRole = 'Pre-SalesConsultant';
            	lstTeamMember.add(oATM);
            	
            	}
           }
           system.debug('@developer-->lstTeamMember:'+lstTeamMember);
           system.debug('@developer-->lstTeamMember.size():'+lstTeamMember.size());
           if(lstTeamMember.size()>0){
           	insert lstTeamMember;
           }
        }
        
    }
}
Maybe this trigger having any issue then let me know.
If Not then mark it solved.
Thanks
Hemant
 
Hemant_SoniHemant_Soni
Hi Bill,
Above Code is for old Owner and below code is for new Owner.
trigger Account on Account (after update) {
	list<AccountTeamMember>lstTeamMember = new list<AccountTeamMember>(); 
    if(trigger.IsAfter){
        system.debug('@developer-->IsAfter');
        if(trigger.IsUpdate){
            system.debug('@developer-->IsUpdate');
            
        RecordType rtAccount = [Select Id,Name,developerName from Recordtype where sObjecttype='Account' And developerName='HouseHold'];
        
        for(Account oAccount:trigger.new){ 
            system.debug('@developoer-->oAccount:'+oAccount);
            system.debug('@developoer-->oAccount.RecordtypeId:'+oAccount.RecordtypeId);
            system.debug('@developoer-->oAccount.OwnerId:'+oAccount.OwnerId);
            system.debug('@developoer-->oAccount.trigger.oldmap.get(oAccount.Id).ownerId:'+trigger.oldmap.get(oAccount.Id).ownerId);
            
            if(oAccount.RecordtypeId == rtAccount.Id && oAccount.OwnerId != trigger.oldmap.get(oAccount.Id).ownerId){
            	
            	AccountTeamMember oATM = new AccountTeamMember ();
            	oATM.AccountId = oAccount.Id;
            	oATM.UserId = oAccount.OwnerId;
            	oATM.TeamMemberRole = 'Pre-SalesConsultant';
            	lstTeamMember.add(oATM);
            	
            	}
           }
           system.debug('@developer-->lstTeamMember:'+lstTeamMember);
           system.debug('@developer-->lstTeamMember.size():'+lstTeamMember.size());
           if(lstTeamMember.size()>0){
           	insert lstTeamMember;
           }
        }
        
    }
}
Maybe this trigger having any issue then let me know.
If Not then mark it solved.
Thanks
Hemant