function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter McGavinPeter McGavin 

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


I am getting the error: 09:59:02:052 FATAL_ERROR System.QueryException: List has no rows for assignment to SObject when trying to complete the Apex Web services challenge.

Here is my code:

a) AccountManager class:

global class AccountManager {
    global static Account getAccount() {
        RestRequest request = RestContext.request;
        String AccId = request.requestURI.substringBetween('/Accounts/' , '/Contacts');
        Account result =  [SELECT Id, Name, (Select Id, Name from Contacts) FROM Account WHERE Id = :AccId];
        return result;

b) AccountManagerTest:
private class AccountManagerTest {

    private static testMethod void getAccountTest1() {
        Id recordId = createTestRecord();
        System.debug('record id = ' + recordId);
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri = ''+ recordId +'/contacts' ;
        request.httpMethod = 'GET';
        RestContext.request = request;
        // Call the method to test
        Account thisAccount = AccountManager.getAccount();
        // Verify results
        System.assert(thisAccount != null);
        System.assertEquals('Test record', thisAccount.Name);

    // Helper method
    static Id createTestRecord() {
        // Create test record
        Account TestAcc = new Account(
            Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
            AccountId =;
        insert TestCon;
        return TestAcc.Id;
Best Answer chosen by Peter McGavin
Gokula KrishnanGokula Krishnan

All Answers

Gokula KrishnanGokula Krishnan
This was selected as the best answer
Peter McGavinPeter McGavin
Thanks Gokula.