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
prani rachaprani racha 

whenever an Account is created or updated Account Team Member Role based on multiple territory id's should be assigned to the team member

whenever an Account is created or updated Account Team Member Role should be assigned to the team member  based on multiple territory id's... please anyone help me out!!!!  how to do this......

Below is the Apex class and trigger for the Account is created   or updated Account Team Member Role should be assigned to the team member  based on single  territory id...

Apex Class:

public class AccountTeamMember_Class{
    public static void accountTeamAssignInsert(List<Account> newList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;
             
        for (Account acc : newList) {
            uid = acc.ownerId;
            for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where email in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                    for(User u : usr){
                        if(u.email == s.User_Email_Address__c ){
                            system.debug('Acc.ownerId:::'+Acc.ownerId);
                            AccountTeamMember atm = new AccountTeamMember();
                            AccountShare  nas = new AccountShare();
                            atm.AccountId = acc.id;                
                            atm.TeamMemberRole = s.TeamMemberRole__c;
                            atm.UserId= u.id; 
                            latm.add(atm);
                            
                           if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){
                             
                                nas.AccountId =acc.id; 
                                nas.UserOrGroupId =u.id;
                                nas.AccountAccessLevel ='Edit';
                                nas.OpportunityAccessLevel = 'Edit';
                                nas.CaseAccessLevel = 'Edit';
                                las.add(nas);
            
                            }
                         }   
                               
                    }    
                }
            }
            
            try{
            
                
                insert(latm);
                insert(las);
            }catch(DMLException e){
        
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
    //=============================================UPDATE====================================================================
    public static void accountTeamAssignUpdate(List<Account> newList,List<Account> oldList){
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        Id uid;
        String oldterritory_id;
        String newterritory_id;
        for(Account a : oldList){
            oldterritory_id = a.Territory_ID__c;
        }
        for(Account a : newList){
            newterritory_id = a.Territory_ID__c;
        }
        if(oldterritory_id != newterritory_id){
            for (Account acc : newList) {
                setids.add(acc.id);
            }
            
            List<AccountTeamMember> oldatm = [select id from AccountTeamMember where AccountId in : setids];
            if(oldatm != null){
                delete(oldatm);
            }
        }
        for (Account acc : newList) {
            uid = acc.ownerId;
            for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where email in : listEmails AND isActive = true];  
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                    for(User u : usr){
                        if(u.email == s.User_Email_Address__c ){
                            system.debug('Acc.ownerId:::'+Acc.ownerId);
                            AccountTeamMember atm = new AccountTeamMember();
                            AccountShare  nas = new AccountShare();
                            atm.AccountId = acc.id;                
                            atm.TeamMemberRole = s.TeamMemberRole__c;
                            atm.UserId= u.id; 
                            latm.add(atm);
                            
                           if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){
                             
                                nas.AccountId =acc.id; 
                                nas.UserOrGroupId =u.id;
                                nas.AccountAccessLevel ='Edit';
                                nas.OpportunityAccessLevel = 'Edit';
                                nas.CaseAccessLevel = 'Edit';
                                las.add(nas);
            
                            }
                        }    
                               
                    }    
                }
            }
            
            try{
                
                insert(latm); 
                system.debug('latm=========='+latm);
                insert(las);    
                system.debug('las=========='+las);                 
            }catch(DMLException e){
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
}

Apex Trigger:

trigger AddAccountTeamMember on Account (after insert,after update) {
      if(Trigger.isInsert){
        AccountTeamMember_Class.accountTeamAssignInsert(trigger.new);
    }
    if(Trigger.isUpdate){
        AccountTeamMember_Class.accountTeamAssignUpdate(trigger.new,trigger.old);
    }
}
Kevin DiLoretoKevin DiLoreto
Sorry if this doesnt work in advance, really didnt have much time to look at this. Assuming your logic is correct, it should work. I removed a couple of your loops, and if statement, and removed the "uid" Id variable. A map should handle all of the removed loops and the if statement. This code will probably make it a little easier to isolate the root cause of the issue. Also, if you post what issue you're encountering it would help... Lastly, I would add some checks to make sure your lists are not null prior to looping through your lists and inserting new records. Let me know if this helps. I couldnt test it, so its possible theres an error...

public class AccountTeamMember_Class{
    public static void accountTeamAssignInsert(List<Account> newList){
        
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        set<id> accids = new set<id>();
        set<string> listEmails = new set<string>();
        map<string,string> EmailToTeamRole_Map = new map<string,string>();
             
        for (Account acc : newList) {
            for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
                 if(acc.Territory_ID__c == s.Territory_ID__c){ 
                     listEmails.add(s.User_Email_Address__c);
                     EmailToTeamRole_Map.put(s.User_Email_Address__c,s.TeamMemberRole__c);
                     system.debug('listemails:::::'+listemails);
                 }
            }
        }
        list<User> usr = [SELECT id,name,email,username,profile.name from user where email in : listEmails AND isActive = true];
        
            
        system.debug('user:::::'+usr);
            for(Account acc : newList){
                            system.debug('Acc.ownerId:::'+Acc.ownerId);
                            AccountTeamMember atm = new AccountTeamMember();
                            AccountShare  nas = new AccountShare();
                            atm.AccountId = acc.id;                
                            atm.TeamMemberRole = EmailToTeamRole_Map.get(acc.email); 
                            atm.UserId= acc.ownerId; 
                            latm.add(atm);
                            
                           if(u.profile.name != 'System Administrator' && u.id != acc.ownerid){
                             
                                nas.AccountId =acc.id; 
                                nas.UserOrGroupId =acc.ownerId;
                                nas.AccountAccessLevel ='Edit';
                                nas.OpportunityAccessLevel = 'Edit';
                                nas.CaseAccessLevel = 'Edit';
                                las.add(nas);
            
                            }
            }
            
            try{
            
                
                insert(latm);
                insert(las);
            }catch(DMLException e){
        
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
}
asda aaa aanasasda aaa aanas

Is this code is workable for dynamic Website? I want to use for my WordPress blog (https://thehomebeasts.com/best-above-ground-pools).