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
Jonathan Osgood 3Jonathan Osgood 3 

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

Hello,

Test coverage at 80% and I cant figure out why. Red highlight on Line 11 "return result;" GET request is working fine in workbench.

Account manager class:
 
@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {

    @HttpGet
    global static Account getAccount() {
        RestRequest request = RestContext.request;
        // grab the accountId from the end of the URL
        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);
        return result;
        
    }
}
Test Class:
 
@IsTest
private class AccountManagerTest {

    @isTest static void testGetAccount() {
        Id recordId = createTestRecord();
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri =
            'https://na1.salesforce.com/services/apexrest/Accounts/'+ 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 accountTest = new Account(
        Name='Test record');
        insert accountTest;
       
        Contact con1 = new Contact(
        AccountId = accountTest.Id,
        firstName = 'test',
        lastName  =  'tester');
        insert con1;
        return accountTest.Id;
    }          

}


 
Best Answer chosen by Jonathan Osgood 3
Bryan JamesBryan James
in the apex class you are testing the urlMapping is case sensitive. So you need to capitalize the C in Contacts
@RestResource(urlMapping='/Accounts/*/Contacts')

All Answers

Bryan JamesBryan James
in the apex class you are testing the urlMapping is case sensitive. So you need to capitalize the C in Contacts
@RestResource(urlMapping='/Accounts/*/Contacts')
This was selected as the best answer
Jonathan Osgood 3Jonathan Osgood 3
Thanks, that got me to 100%. I'm still getting an error checking the challenge in Trailhead though. I've tried clear tests and "run all" with no luck.

User-added image
Bryan JamesBryan James
So I found the trail you were doing and make sure that the urlMapping matches the one that they request you do. so it should look like '/Accounts/*/contacts' in all places. so same thing. it is case sensitive but they are looking for a lower case c. 
Jonathan Osgood 3Jonathan Osgood 3
That did it, thanks!
teju kumarteju kumar
Thanks for the solution sor, facing this problem since 3 years
whatsappaero (https://whatsappaero.xyz/)
Bruno Araujo 2Bruno Araujo 2
You are welcome! 
My blog (https://expositoryessays.org)
Sourav biswas 1Sourav biswas 1
Thank you so much for this valuable super content likely i write a post about Naseeb status in hindi (https://www.topics-guru.com/2020/04/Naseeb-Status-in-Hindi.html) , you can read this awesome post.
and if you want more shayari (https://www.topics-guru.com/search/label/Shayari?&max-results=5) then just visit our website Caption and slogan (https://www.topics-guru.com/) Homepage.