You need to sign in to do that
Don't have an account?

Having System.QueryException: List has no rows for assignment to SObject
Hi,
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
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
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];
or
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.
Regards,
Mahesh
@RestResource(urlMapping='/Accounts/<Account_ID>/contacts')
global with sharing class AccountManager {
@HttpGet
global static Account[] getAccount() {
RestRequest request = RestContext.request;
String AccountId = request.requestURI.substring(
request.requestURI.lastIndexOf('/')+1);
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;
}
}
@IsTest
private class AccountManagerTest {
@isTest static void testGetAccountById() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri =
'https://antonioucorp.salesforce.com/services/apexrest/Account/'
+ 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';
NombreRecup=cont.Name;
System.assertEquals(NombreContac,NombreRecup,'El nombre del contacto no existe');
//System.assertEquals(Cuenta[0].ID,cont.AccountId,'La cuenta no coincide');
j++;
}
}
// 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;
}
}
Error:
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.