1

I can't seem to figure out how to reach 100% code coverage. Currently, I'm at 80% and the "return acct;" is the only thing that is not covered. Could somebody help me figure out how to test this? The error/fail in debug is: "System.QueryException: List has no rows for assignment to SObject" and the Stack Trace is: "Class.AccountManager.getAccount: line 10, column 1 Class.AccountManagerTest.testGetAccount: line 11, column 1"

ACCOUNT MANAGER CLASS:

@RestResource (urlMapping = '/Account/*/contacts')
global with sharing class AccountManager
{
    @HttpGet
    global static Account getAccount ()
    {
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
        //Account acct =  [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account WHERE Id = :accountId];
        Account acct = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        return acct;
    }
}

ACCOUNT MANAGER TEST:

@isTest
private class AccountManagerTest
{
    @isTest static void testGetAccount ()
    {
        Id recordId = createTestRecord ();
        RestRequest request = new RestRequest ();
        request.requestUri = 'https://yourInstance.salesforce.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 testAccount = new Account (Name = 'Test Record');
        insert testAccount;
        Contact testContact = new Contact (AccountId = testAccount.Id);
        return testAccount.Id;
    }

}

Thank you!

2

Your code looks for the Id thus:

String accountId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);

You set the URI thus:

'https://yourInstance.salesforce.com/services/apexrest/Accounts/' + recordId + '/contacts'

So your code is filtering:

WHERE Id = 'contacts'

Change your URI to remove + '/contacts'.

| improve this answer | |
  • Okay, thank you. That makes sense: I'm asking for whatever comes after the last "/", and in this case it is "contacts" so I am basically saying accountId = "contacts". When I removed that last bit of code I did achieve 100% coverage. However, when I tried to check the challenge it said that "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". After looking around I realized that this could be because the query is coming back empty. Why? – Elizabeth Rodriguez Jul 31 '18 at 16:24
0

You should leave the URI as

`.....apexrest/Accounts/' + recordId + '/contacts'

but extract the account recordId like this:

String accountId = request.requestURI.substringBetween('Accounts/', '/contacts');

Kudos to Lei Shi at https://developer.salesforce.com/forums/?id=906F0000000MIs9IAG

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.