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
Nagnath Mane 11Nagnath Mane 11 

Problem with Apex Rest web service challenge.

Getting  Error:
Executing the 'AccountManager' method failed. Either the service isn't configured with the correct urlMapping, is not global, does not have the proper method name or does not return the requested account and all of its contacts.

@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;
    }
}

Test class

@isTest 
private class AccountManagerTest{
    static testMethod void testMethod1(){
        Account objAccount = new Account(Name = 'test Account');
        insert objAccount;
        Contact objContact = new Contact(LastName = 'test Contact',
                                         AccountId = objAccount.Id);
        insert objContact;
        Id recordId = objAccount.Id;
        RestRequest request = new RestRequest();
        request.requestUri =
            'https://mindful-otter-12kfkn-dev-ed.lightning.force.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 Account', thisAccount.Name);
    }
}
Vishwajeet kumarVishwajeet kumar
Hi,
Looks like Test Class is not using HttpCalloutMock.
Checkout this link - https://developer.salesforce.com/forums/?id=906F0000000AsVFIA0  , it has a test class for Rest Service.

Thanks.
ANUTEJANUTEJ (Salesforce Developers) 
Hi Nagnath,

For all the Certification and Trailhead Guidance please report it here,

https://trailhead.salesforce.com/en/help?support=home

https://trailhead.salesforce.com/help

So that our trailhead support engineers will look into it and get back to 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.

Regards,
Salesforce Support.
Nagnath Mane 11Nagnath Mane 11
I found the solution.
After checking the callengeing i looked into the logs and found the error: https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IUJb
from the error log i got the Account ID and i created contact for this account id and able to test the challenge successfully.
Shivangi VermaShivangi Verma
@Nagnath Apologies  meant to like your suggestion but there seems to be some issue with dev forms. Thanks a lot for your input, it helped me clear my challenge for which i was struggling since many days.