0

I was working on the Web Services trailhead which is failing because my class does not have 100% coverage. The only line that is not being covered is the return statement.

@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accId = req.requestURI.substringBetween('Accounts/', '/contacts');
        Account acc = [SELECT Id, Name, (SELECT Id, Name FROM Contacts) 
                       FROM Account WHERE Id = :accId];
        return acc;
    }
}

Why would this line not be included in code coverage?

1 Answer 1

2

Odds are, your query returned no rows, and an exception was thrown. Make sure that your code is properly getting the ID out of the URL, and that it is set correctly. Also, I generally recommend that you don't use temp variables when you have no need to do so:

@RestResource(urlMapping='/Accounts/*/contacts')
global class AccountManager {
    @HttpGet
    global static Account getAccount() {
        Id accId = RestContext.request.requestURI
            .substringBetween('Accounts/', '/contacts');
        return [SELECT Id, Name, (SELECT Id, Name FROM Contacts) 
                FROM Account WHERE Id = :accId];
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .