I'm trying to complete the Apex Integration > Apex Web Services Challenge and getting the following error:
Executing the method 'getAccount' from the Apex class 'AccountManager' 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.
I verified the easy ones - the class is set to global, the url mapping is correct, and the class names are correct. Also, when I run it via the Workbench Rest Explorer, I get back data from the class, so I know the class works the way I expect it to, so I have to assume the issue is in my test case.
Here's the code I'm using
@RestResource(urlMapping='/Account/*/contacts')
global class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest request = RestContext.request;
// grab the caseId from the end of the URL
String accountId = request.requestURI.substring(
request.requestURI.lastIndexOf('/Account/')+9,
request.requestURI.lastIndexOf('/'));
System.debug('accountId = ' + accountId);
Account account = [SELECT Id,NAME,(SELECT Id,NAME FROM CONTACTS) FROM ACCOUNT WHERE ID = :accountId];
System.debug(account);
return account;
}
}
@isTest
public class AccountManagerTest {
@isTest
static void testGetAccount() {
Id recordId = createTestRecord();
System.debug('Id = ' + recordId);
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri = 'https://resilient-otter-s03cy5-dev-ed.trailblaze.my.salesforce.com/services/apexrest/Account/' + recordId + '/contacts';
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Account thisAccount = AccountManager.getAccount();
// Verify results
System.assert(thisAccount != null);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account accountTest = new Account(
Name='Test Account');
insert accountTest;
Contact contactTest = new Contact(FirstName='Test Account',LastName='Test Contact',AccountId=accountTest.Id);
insert contactTest;
return accountTest.Id;
}
}
I can't figure out what I've done wrong here. Any advice would be appreciated.
I was able to figure out what I had done wrong.
The requirements called for the word Accounts in the URI for the resource. I was only using the word Account.
Once I corrected that, I was able to get the system to recognize the resource and run correctly.