+ Start a Discussion
Veena GopalVeena Gopal 

trailhead apex web services

the question in the challenge is:The method must return the ID and Name for the requested record and all associated contacts with their ID and Name.

but ppl have passed the trailhead by using similar code:
Account result =  [Select Id,Name ,(Select Id, Name From Contacts) From Account where Id = :accId];
        return result;

When i put this in the execute anonymous window:-
Account result =  [Select Id,Name ,(Select Id, Name From Contacts) From Account where Id = '00128000005ee4zAAA'];
system.debug(result);

I get only the account id i do not get the contact ids related to it.
How to go about doing it?
Please help. 
Amit Singh 1Amit Singh 1
Have you checked that You have related contact to this "00128000005ee4zAAA" Account Id. Please post the screenshot of Query Editior and of Contact which is related to this Account
Veena GopalVeena Gopal
Hello Amit consider that existing and answer question. I would not be so foolish to ask without having contacts associated. Go and try in your system u will know. Do not reply without trying it in your system.
Amit Singh 1Amit Singh 1
Hi Veena,

Sorry for late response actualy I was in my home town.

As per your response I have tried in multiple orgs min 10 and got the same result means all accounts with contacts.

Here is my class that I have used for the challenge hope this will help.
@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 {

    private static testMethod void getAccountTest1() {
        Id recordId = createTestRecord();
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri = 'https://na1.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 TestAcc = new Account(
          Name='Test record');
        insert TestAcc;
        Contact TestCon= new Contact(
        LastName='Test', 
        AccountId = TestAcc.id);
        return TestAcc.Id;
    }      
}


 
Linked2MarkLinked2Mark

Hi - I could use some input on the CaseManager code that is at the beginning of the trailhead.

I enter the code exactly as the trailhead describes abd attempt to hit the '/services/apexrest/Cases/' url with workbench but I get the following error. What am I missing?

Service not found at: /services/apexrest/Cases/

 

Philippe SimoesPhilippe Simoes
I know it was asked a long time ago, but in case someone read this, here is the answer: 

You can make a loop using : result.contacts.size()
Then access all contacts using this format : result.contacts[0].Id;

Or another way: 

for (Contact ct : result.contacts)
{
    system.debug(ct.id);
}