Skip to main content
I've completed the challenge, it has 100% coverage. I've checked all the method names. The URL is valid. I've used Work Bench and curl to test and even tested with multiple Accounts with and without contacts.

I know on other challenges, punctionation was important.  What about the defination of the return? Are there expected names?

I built a class to hold Account ID & Name along with a List of Contact names and IDs. Is this my issue?  Anyone else have a challenge with this challenge?

Any help or hints will be appreciated.

Here are snippets of my code:

@RestResource(urlMapping='/Accounts/*/contacts')

global with sharing class AccountManager {

....

global class APIAccount {

        public ID Id;

        public String Name;

        List<APIContact> Contacts;

...

@HttpGet

    global static APIAccount getAccount() {

        RestRequest request = RestContext.request;

...

 
32 answers
  1. Dec 22, 2015, 4:54 AM
    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;

    }

    }

     
  2. Jan 15, 2016, 4:45 PM

    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,

    Esteve

     
  3. Jul 13, 2016, 12:20 PM
    I 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.

     
  4. Jan 25, 2018, 4:39 AM
    @jayanth B 2

    Thanks 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.

     
  5. Feb 5, 2018, 2:45 PM

    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. 

  6. Sep 27, 2017, 7:17 AM
    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

    @isTest

    public 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();

        }

    }
  7. Dec 2, 2020, 3:41 PM

    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.
  8. Apr 17, 2020, 10:13 AM
    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');

     
  9. Oct 28, 2018, 5:42 AM
    For me it was @Charles Thompson's suggestion that worked. I was missing the 's' in contacts. Thanks Charles!
  10. May 23, 2018, 12:53 AM
    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.
0/9000