You don't have to create a separate Class or a Map. Below please find my working code, and be careful with the lower case for 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;
}
}
For the trailheaders with problemas getting validation on this exercise:
- The result must be an account (its how I passed) as Lei Shi pointed it out
- Be very very careful with no records returned from your query as could a nightmare as I faced. Solved by adding: @isTest(SeeAllData=true)
I think it is very woth, at least it was for me to read this article about this: Demystifying SeeAllData in Unit Tests
http://opfocus.com/blog/demystifying-seealldata-in-unit-tests/.All the best,EsteveI spent ~ an hour to solve this mystery.I looked at the developer console log after ruinning the challange and found '/contacts' (lower case) was passed by the engine.replacing '/Contacts' --> '/contacts' in my code solved my problem.I may help somebody. @jayanth B 2Thanks for posting "optimised" code. Next time, could you mark your code as a code block (in the WYSIWYG editor)? That makes it easier for future generations to read. For all who come in the future, the challenge requires the class to start with
@RestResource(urlMapping='/Accounts/*/contacts')
Note the trailing "/contacts". The challenge also requires you to specify /contacts on the end of your REST URI.
The class actually doesn't require this, but this unit doesn't make it clear. You *can* use the urlMapping without '/contacts' and the resulting data is almost identical. Unfortunately, the challenge doesn't mention this, nor does it test for this simpler (i.e. more easily maintained) usage. In fact, the challenge should make it clear exactly what type the return result should be and what the URI construction should be.For anyone else who tries everything here but still gets this message ...
Methods defined as TestMethod do not support Web service callouts
My problem was that I had a couple test triggers on my Account and Contact objects and for some reason those were causing my Test for this lesson to fail. Soon as I deactivated those triggers the Test passed.
Optimized code, refer in case required.@RestResource(urlMapping='/Accounts/*/contacts')global class AccountManager { @HttpGet global static Account getAccount(){ RestRequest request = RestContext.request; return [Select id, name,(SELECT id,name from Contacts) from Account where id=:request.requestURI.substringBetween('Accounts/', '/contacts')];}}Test class@isTestpublic class AccountManagerTest { @isTest public static void executeGetAccount(){ //making Data ready for Testing Account acc = new Account(Name='Jayanth B'); insert acc; insert new Contact(LastName='Jayanth',FirstName='b',AccountId=acc.id); // Set up a test request RestRequest request = new RestRequest(); request.requestURI = 'https://ap4.salesforce.com/services/apexrest/Accounts/'+(String)acc.id+'/contacts'; request.httpMethod ='GET'; //set up request Context RestContext.request = request; // call the method Account returnedAcc = AccountManager.getAccount(); }} Most of the errors in this challenge are related to the Url Mapping, it has to be called exaclty the same way it was defined.
urlMapping='/Accounts/*/contacts'
This is case sensitive, so '/accounts/1234/Contacts' will not work, etc.
Good luck.I also spent an extra hour because it wasn't clear about the '/contacts' at the end of the request. I found it by debugging and arrived at the same solution as Lei, Charles and Mustafa. I used the following to extract the record Id:
Integer subend = request.requestURI.lastIndexOf('/');
Integer subst = 1 + request.requestURI.lastIndexOf('/',subend-1);
accntId = request.requestURI.substring(subst,subend);
but I much prefer Lei's simple solution:
accId = request.requestURI.substringBetween('Accounts/', '/contacts');
For me it was @Charles Thompson's suggestion that worked. I was missing the 's' in contacts. Thanks Charles! For anyone who might get stuck:Its not very clear in instructions, that you have to make a test record for account in your test as Test methods cannot read your Organization data.So either you create some test resords in test class or enable see all data (@IsTest(SeeAllData=true)). This will allow to read org data in your test method.
32 answers