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
Siddharth . 2Siddharth . 2 

Hi,I was solving a trailhead challenge for Apex Web Services.I am unable to pass the challenge as the error shows is below.

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

Stack Trace:
Class.AccountManager.getAccount: line 10, column 1
Class.AccountManagerTest.TestGetById: line 19, column 1

Account ManagerClass:

@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
    
    @HttpGet
    global static Account getAccount(){
        
        RestRequest request=RestContext.request;
        string AccountId=request.requestURI.substringBetween('Accounts','contacts');
        
        Account accountdetails = [Select Id,name,(Select Id,Name from Contacts) from Account where Id=:AccountId LIMIT 1];
        return accountdetails;
    }

}

AccountManagerTest:

@isTest
public class AccountManagerTest {
    

 @isTest static void TestGetById(){
        
        Account testaccount = new Account(name='TestAccount');
            insert testaccount;
        Contact objContact = new Contact(LastName ='TestContact',AccountId=testaccount.Id);
         insert objContact;
        id recordid=testaccount.Id;
        
        Restrequest request=new Restrequest();
        request.requestURI='https://ap8.lightning.force.com/services/apexrest/Account/'+recordid+'/contact';
        request.httpMethod='GET';
        
        RestContext.request=request;
        
        Account thisAccount=AccountManager.getAccount();
        //Verify the results
        System.assert(thisAccount!= null);
        System.assertEquals('TestAccount', thisAccount.Name);

        
        
    }

}
Khan AnasKhan Anas (Salesforce Developers) 
Hi Siddharth,

Greetings to you!

There is a problem in URI, it is not resolving the URI. Changed the above code to below and it will work.

AccountManager Class:
@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount(){
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substringBetween('Accounts/','/contacts');
        Account Result = [SELECT Id, Name, (SELECT Id, Name From Contacts) From Account Where Id =: accountId];
        system.debug('Result is :' +Result);
        Return Result;
    }
}

AccountManagerTest Class:
@isTest
private class AccountManagerTest {
    private static testMethod void testGetAccount(){
        Id recordId = createTestRecord();
        RestRequest request = new RestRequest();
        request.requestUri = 'https://ap8.lightning.force.com/services/apexrest/Accounts/'+recordId+'/Contacts';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account thisAccount = AccountManager.getAccount();
        System.assert(thisAccount != null);
        System.assertEquals('Test record', thisAccount.Name);
    }
    static Id createTestRecord(){
         Account TestAcc = new Account(
          Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
        LastName='Test', 
        AccountId = TestAcc.id);
        return TestAcc.Id;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Siddharth . 2Siddharth . 2
Hi
Thank you for sorting this out.Could you please let me know why do I have to create a method to create a new record is  the test class.Why I should have not used the Id generated by creating the object as I did in my first approach. 
Eagerly waiting for your answer.
Khan AnasKhan Anas (Salesforce Developers) 
You can use any approach but make sure URI should be correct.

Happy Learning!
Siddharth . 2Siddharth . 2
Hi 

I have tried the code provided by you but it still give the same error.