You need to sign in to do that
Don't have an account?
Nagnath 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);
}
}
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);
}
}
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.
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.
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.