You need to sign in to do that
Don't have an account?

Apex Web Services Challenge - unclear on return format
The instructions state:
"The method must return the ID and Name for the requested record and all associated contacts with their ID and Name."
I am returning a List<sObject> where the first object is the Account and the remaining objects are the Contacts.
The error message is:
"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."
I assume it is looking for a different structure to be returned by getAccount() besides List<sObject> but since Account doesn't have a field to store associated Contacts, I'm not sure what to return.
Anyone able to clarify for me what type & structure the return value should have?
"The method must return the ID and Name for the requested record and all associated contacts with their ID and Name."
I am returning a List<sObject> where the first object is the Account and the remaining objects are the Contacts.
The error message is:
"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."
I assume it is looking for a different structure to be returned by getAccount() besides List<sObject> but since Account doesn't have a field to store associated Contacts, I'm not sure what to return.
Anyone able to clarify for me what type & structure the return value should have?
All Answers
Based on your reply, I see I need a relationship query on Account instead, and now it works. Thanks!
This worked for me !!!
@RestResource(urlMapping='/Accounts/*')
global with sharing class AccountManager
{
@HttpGet
global static Account getAccount()
{
RestRequest request = RestContext.request;
List<String> lst = request.requestURI.split('/');
String strAccId = lst[lst.size() - 2];
ResponseWrapper objResp = new ResponseWrapper();
Account obj = [select Id,Name ,(select Id,Name from Contacts) from Account where Id=:strAccId limit 1];
return obj ;
}
}
Regards,
Ajay
Does the test environment have access to existing records? The following excerpt from "Getting Started with Apex Unit Tests" is ambiguous - does a "copy" mean a copy of the data or a copy of the org's metadata so it has knowlege of the schema, meaning we always have to create our data within the test? If so, what is the scope of the data, i.e., only limited to a unit test or to a class?
"Maintaining the security of your data is our highest priority. We don't view or modify any data in your org, and all testing is done in a copy that runs in a secure data center."
For reference I use the following method to retrieve data:
I keep getting the following error message eventhough I do "Run All", please help :)
Challenge not yet complete... here's what's wrong:
The 'AccountManager' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge.
Seems your 'AccountManager' class didn't covered 100% of code coverage. We have to click on 'Run Test' button in that 'AccountManagerTest' class and check whether you covered the class 100% or not.
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 with sharing class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest request = RestContext.request;
String accountId = request.requestURI.substringBetween('/Accounts/','/contacts');
Account result = [SELECT Id, Name, (SELECT Id, Name from contacts) from Account where Id=:accountId];
return result;
}
}
============================
@IsTest
private class AccountManagerTest {
@isTest static void testGetContactsByAccountId() {
Id recordId = createTestRecord();
// Set up a test request
RestRequest request = new RestRequest();
request.requestUri =
'https://yourInstance.salesforce.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 record', thisAccount.Name);
}
// Helper method
static Id createTestRecord() {
// Create test record
Account accountTest = new Account(
Name='Test record');
insert accountTest;
Contact contactTest = new Contact(
FirstName = 'John',
LastName = 'Doe',
AccountId = accountTest.Id);
insert contactTest;
return accountTest.Id;
}
}
======================================
I spent more than one hour checking everything. I hope you have the same problem.
thank you
Mine was apparently failing because I used a capital C for "Contacts" in my urlMapping, like this:
The code worked (I don't remember what all is case-sensitive, but I was consistent across code and test class with "C") and the test worked and I had 100% test coverage but I was still failing. When I changed all my "Contacts" to "contacts" I passed the challenge. :/
change this line:
To this:
My trailhead playground has the URI as '.../Accounts/<id>/Contacts', whereas the verification test for the challenge has it as '.../Accounts/<id>/contacts'. So if you pass the test, you get an error on the challenge, whereas if your code could pass the challenge, it will fail the unit test (and thus the challenge too). Just using the forward slash to find the substring eliminates the issue.
You can also try below codes to pass the challenge. it works for me.
AccountManager.apxc
@RestResource(urlMapping='/Accounts/*/contacts')
Global with sharing class AccountManager {
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
//Grab the accountId from end of URL
String accountId = request.requestURI.substringBetween('Accounts/','/contacts');
Account acc = [select Id,Name,(select Id,Name from Contacts) from Account where Id = :accountId];
system.debug('Account and Related Contacts->>>>'+acc);
return acc;
}
}
AccountManagerTest.apxc
@isTest
private class AccountManagerTest {
//Helper method to create dummy record
static Id createTestRecord(){
//Create test record
Account TestAcc = new Account(Name='Test Account', Phone='8786757657');
insert TestAcc;
List<Contact> conList = new List<Contact>();
Contact TestCon = new Contact();
for(Integer i=1;i<=3;i++){
TestCon.LastName = 'Test Contact'+i;
TestCon.AccountId = TestAcc.Id;
//conList.add(TestCon);
insert conList;//Its not best practice but I have use it for testing purposes
}
//insert conList;
//insert TestAcc;
return TestAcc.Id;
}
//Method to test getAccount()
@isTest static void getAccountTest(){
Id recordId = createTestRecord();
//setup a test request
RestRequest request = new RestRequest();
//set request properties
request.requestURI = 'https://yourInstance.salesforce.com/services/apexrest/Accounts/' + recordId +'/contacts';
request.httpMethod = 'GET';
// Finally, assign the request to RestContext if used
RestContext.request = request;
//End test setup
//Call the method
Account thisAcc = AccountManager.getAccount();
//Verify the result
system.assert(thisAcc != null);
system.assertEquals('Test Account', thisAcc.Name);
//system.assertEquals(3, thisAcc.Contact__c.size()); how to get this
}
}