• prani racha
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 8
    Questions
  • 1
    Replies
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);
    }
}
 
Hi , I have written  apex class and trigger for territory management piece where whenever account is created based on territory ID then it should query custom settings table and should assign temebers roles to the team members that are having the same territory id and also the email address should map the mail address in salesforce users list.
Class, trigger, test class are working fine and I am getting the error:
               Sandbox

Apex script unhandled exception by user/organization: 005q0000000Qlme/00Dq00000009Orr
Source organization: 00DU0000000LYdk (null)
Failed to process batch for class 'rh2.PS_Rollup_Batch' for job id '707q0000002UIGr'

caused by: System.LimitException: Too many DML rows: 10001

Class.AccountTeamMember_Class.accountTeamAssignUpdate: line 125, column 1
Trigger.AddAccountTeamMember: line 6, column 1
 
Whenever batch class runs every one hour am getting this error
Please anyone help me out how to make my trigger bulk friendly and suggest me how to resolve this issue.

Apex class and trigger:


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){
                    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(las);
                insert(latm);
             
                       
            
            }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){
                    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(las);
                insert(latm);
             
                       
            
            }catch(DMLException e){
        
                    system.debug('error on insert team members ' + e.getMessage());
            }  
            
    }
}

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);
    }
}
I am getting this error:

Atmember: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Class.ATeam_Cls.teamassign: line 66, column 1 Trigger.Atmember: line 2, column 1

class: public class ATeam_Cls {
        
    public static void teamassign(List<Account> newList){
        
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        List<User> us = [Select id,userrole.Name,username,Email,profile.name from User where IsActive = true];
        
        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) {
        
        ID uid = acc.OwnerId;
        
        for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
            
            if(acc.Territory_ID__c == s.Territory_ID__c){ 
                
                for(User u : us){
                
                    if(u.Email == s.User_Email_Address__c ){
        
                        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'){
                         
                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);
        
                        }
                       
                      
                    }
                }  
            }
        } 
       }
       
        
        try{
            
            insert(las);
            insert(latm);            
            
        }catch(DMLException e){
        
                    system.debug('error on deleting team members ' + e.getMessage());
        } 
        user usr = [select id,name,UserRole.Name from user where id =: userInfo.getUserId()];
    
    for(Account acc : newList){
       AccountTeamMember atm1 = new AccountTeamMember();
    
        atm1.AccountId = acc.id;                
        atm1.TeamMemberRole = usr.UserRole.Name;
        atm1.UserId=UserInfo.getUserId(); 
    
        try{
        
            insert atm1;
        }catch(DMLException e){
        
                    system.debug('error on deleting team members ' + e.getMessage());
        }

    
    }
    }
}

Trigger:

trigger Atmember on Account (after insert,after update) {
    ATeam_Cls.teamassign(trigger.new);
}

above are the class and trigger . please anyone help me out how can it be solved
I am getting this error:

Atmember: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject Class.ATeam_Cls.teamassign: line 66, column 1 Trigger.Atmember: line 2, column 1

class: public class ATeam_Cls {
        
    public static void teamassign(List<Account> newList){
        
        List<AccountTeamMember> latm = new List<AccountTeamMember>();
        List<AccountShare> las = new List<AccountShare>();
        set<id> setids = new set<id>();
        List<User> us = [Select id,userrole.Name,username,Email,profile.name from User where IsActive = true];
        
        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) {
        
        ID uid = acc.OwnerId;
        
        for(IDEXX_Water_Territories__c s : IDEXX_Water_Territories__c.getAll().values()){
            
            if(acc.Territory_ID__c == s.Territory_ID__c){ 
                
                for(User u : us){
                
                    if(u.Email == s.User_Email_Address__c ){
        
                        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'){
                         
                            nas.AccountId =acc.id; 
                            nas.UserOrGroupId =u.id;
                            nas.AccountAccessLevel ='Edit';
                            nas.OpportunityAccessLevel = 'Edit';
                            nas.CaseAccessLevel = 'Edit';
                            las.add(nas);
        
                        }
                       
                      
                    }
                }  
            }
        } 
       }
       
        
        try{
            
            insert(las);
            insert(latm);            
            
        }catch(DMLException e){
        
                    system.debug('error on deleting team members ' + e.getMessage());
        } 
        user usr = [select id,name,UserRole.Name from user where id =: userInfo.getUserId()];
    
    for(Account acc : newList){
       AccountTeamMember atm1 = new AccountTeamMember();
    
        atm1.AccountId = acc.id;                
        atm1.TeamMemberRole = usr.UserRole.Name;
        atm1.UserId=UserInfo.getUserId(); 
    
        try{
        
            insert atm1;
        }catch(DMLException e){
        
                    system.debug('error on deleting team members ' + e.getMessage());
        }

    
    }
    }
}

Trigger:

trigger Atmember on Account (after insert,after update) {
    ATeam_Cls.teamassign(trigger.new);
}

above are the class and trigger . please anyone help me out how can it be solved