+ Start a Discussion
prani rachaprani racha 

System.QueryException:

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
jp1234jp1234
Does the line 66 of ATeam_Cls refer to the following?
 
user usr = [select id,name,UserRole.Name from user where id =: userInfo.getUserId()];

 
prani rachaprani racha
Yes
 
jp1234jp1234
Looks like that row is running zero record.  Do you have UserRole assigned to your user?
phoenix7788phoenix7788

user usr = [select id,name,UserRole.Name from user where id =: userInfo.getUserId()];         ×
user usr = [select id,name,UserRole.Name from user where id =: userInfo.getUserId()][0];     ○

the result of [select id,name,UserRole.Name from user where id =: userInfo.getUserId()]  is List,
but [user usr] is SObject .
jp1234jp1234
Actually, phoenix7788, that is not true.  If the result of SOQL statement returns a single record (e.g. specifying the ID of the record), it can be parsed to an instance of an sObject.  See https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_single_row.htm