+ Start a Discussion
Antonio Úbeda MonteroAntonio Úbeda Montero 

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


I have this error in AccountManager Trailhead's. But all test are passed and there are a few rows returned.

Anyone knows what can be the problem? Thank you!

Challenge Not yet complete... here's what's wrong:
There was an unexpected error in your org which is preventing this assessment check from completing: System.QueryException: List has no rows for assignment to SObject
In the soql query you have in code, check atleast one record meets the criteria in where clause
Mahesh DMahesh D
Hi Antonio,

There are 2 ways to write the SOQL query.

(1) You can capture the results into a list even variable like below:

List<Account> accList = [Select Id, Name from Account];
List<Account> accList = [Select Id, Name from Account LIMIT 1];

You will never get the above error.

(2) You can capture the results into a single object like below when you want to retrieve only one record.

Account acc = [Select Id, Name from Account LIMIT 1];

This will give you an error if there are no records in the system or if you have where condition and if there is no record matching to the where clause.

Antonio Úbeda MonteroAntonio Úbeda Montero
Yes, i have already done this, but altough the test run properly, don't let me pass the challenge. This is the code:

global with sharing class AccountManager {
    global static Account[] getAccount() {
        RestRequest request = RestContext.request;

        String AccountId = request.requestURI.substring(
        System.debug('##Request recuperada:   '+request);
        System.debug('##Acount id recuperada: '+AccountId);
        Account[] result = [SELECT ID,Name,(SELECT ID,Name
                                          FROM Contacts)
                            FROM Account
                            WHERE ID=:AccountId];  
        Contact[] contactos=result[0].Contacts; 
        System.debug('##Nombre Cuenta: '+result[0].Name);
        System.debug('##Nombre Contacto: '+contactos[0].Name);
        return result;

private class AccountManagerTest {
@isTest static void testGetAccountById() {
        Id recordId = createTestRecord();    
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri =
            + recordId;
        request.httpMethod = 'GET';
        RestContext.request = request;
        // Call the method to test
        System.debug('###Antes de llamada');
        Account[] Cuenta = AccountManager.getAccount();
        System.debug('##Nombre de cuenta: '+Cuenta[0].Name);
        Contact[] ContTest=Cuenta[0].Contacts;
        System.debug('##Nombre del primer contacto: '+ContTest[0].Name);
        System.debug('###Despues de llamada');
        // Verify results
        System.assertEquals('Cuenta prueba', Cuenta[0].Name,'La cuenta no es la esperada');
        List<Contact> contactoRecup = Cuenta[0].Contacts;
        System.assertEquals(5, contactoRecup.size(),'No son cinco elementos');
        Integer j=0; String NombreContac,NombreRecup;
        for (Contact cont: contactoRecup){
            NombreContac='Contacto '+j+' Normal';
            System.assertEquals(NombreContac,NombreRecup,'El nombre del contacto no existe');
            //System.assertEquals(Cuenta[0].ID,cont.AccountId,'La cuenta no coincide');
    // Helper method
    static ID createTestRecord() {
        // Create registros de pruebas        
        Account cuentaPrueba = new Account();
        cuentaprueba.Name='Cuenta prueba';
        insert cuentaPrueba;
        List<Contact> listaCon = new List<Contact>();
        for (Integer i=0;i<5;i++){
            listaCon.add(New Contact(FirstName='Contacto '+i,LastName='Normal',AccountID=cuentaPrueba.id));               
        insert listaCon;                
        return cuentaPrueba.ID;

Challenge Not yet complete... here's what's wrong: 
Executing the 'AccountManager' method failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.