+ Start a Discussion
Meena25Meena25 

FATAL_ERROR System.QueryException: List has no rows for assignment issue to SObject

ownerUser = [Select Id, UserRoleId from user where UserRoleId IN :lstUser AND Id = :opp.OwnerId]; 

This is causing me the exception, what is wrong with this?

Raj VakatiRaj Vakati
Looks like you dnt have any user associated with the user role and opportunity id mismatch .. 


If it a test class insert the data to meet the above SOQL condition .. 


If its a class you can able to handle like below 
 
List<User> ownerUser = [Select Id, UserRoleId from user where UserRoleId IN :lstUser AND Id = :opp.OwnerId];

If(ownerUser.size()>0){
//YOUR logic
}

 
Meena25Meena25
Thanks, However am facing a script-thrown exception now
Meena25Meena25
If (ownerUser != null){
            AuraHandledException e = new AuraHandledException('The application is already with ABC team');
               e.setMessage('The application is already with ABC team');
               system.debug(e);
            throw e;
Raj VakatiRaj Vakati
Include them in try and catch 

 
try{
 If (ownerUser != null){
}
}catch(Exception e){

}

 
Meena25Meena25
I cannot use it in try catch as i have an else part where i have my code logic. Entire Code below:
public class classname {
    @AuraEnabled
    public static Opportunity updateoppOwner(String oppId) {
        List<User> ownerUser;
        Opportunity opp = [Select Name,OwnerId From Opportunity where Id = :oppId];
        List<UserRole> lstUser = [Select Id,Name from userRole where Name LIKE 'ABC%'];
        If (lstUser != null && lstUser.size()>0){
               ownerUser = [Select Id, UserRoleId from user where UserRoleId IN :lstUser AND Id = :opp.OwnerId];
            //system.debug('ownerUser:' +ownerUser);
        }
        If (ownerUser != null){
            AuraHandledException e = new AuraHandledException('The application is already with team');
                               e.setMessage('The application is already with team');
                               system.debug(e);
               throw new AuraHandledException(e.getMessage());
            //throw new AuraHandledException('The application is already with team');
        }
            else           
            {
                User usr = [Select Id, Name from User where Name = 'ABC' limit 1];
                               opp.OwnerId = usr.Id;
                               system.debug('Owner record has been updated');
                               update opp;
            }
    return opp;
    }
}
Raj VakatiRaj Vakati
You have to handle this exception now the Lightning component JS controller 
public class classname {
    @AuraEnabled
    public static Opportunity updateoppOwner(String oppId) {
        List<User> ownerUser;
        Opportunity opp = [Select Name,OwnerId From Opportunity where Id = :oppId];
        List<UserRole> lstUser = [Select Id,Name from userRole where Name LIKE 'ABC%'];
        If (lstUser != null && lstUser.size()>0){
               ownerUser = [Select Id, UserRoleId from user where UserRoleId IN :lstUser AND Id = :opp.OwnerId];
            //system.debug('ownerUser:' +ownerUser);
        }
        If (ownerUser != null){
		 
            AuraHandledException e = new AuraHandledException('The application is already with team');
                               e.setMessage('The application is already with team');
                               system.debug(e);
               throw new AuraHandledException(e.getMessage());
		 
			   
            //throw new AuraHandledException('The application is already with team');
        }
            else           
            {
                User usr = [Select Id, Name from User where Name = 'ABC' limit 1];
                               opp.OwnerId = usr.Id;
                               system.debug('Owner record has been updated');
                               update opp;
            }
    return opp;
    }
}
 
action.setCallback(this, function(response){
                var state = response.getState();
                if(component.isValid()){
                    if (state == "SUCCESS") {
                       
                    }
                    else if (state === "ERROR") {
                      // Handler Error here 
                    }
                }
            });

 
Meena25Meena25
I might need to rebuild the whole logic in a different way now since this error handling is not working.. Else part handles updating opp field.