+ Start a Discussion
FabioMarcobelliFabioMarcobelli 

Problem with the "Apex Web Services" unit

Hi everyone,

i'm getting the following error after the system checks the challenge:

"Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.QueryException: List has no rows for assignment to SObject"

I already tested it via Apex and via REST Explorer in Workbench, the code seems fine to me:

@restResource(urlMapping='/Account/*/contacts')
global class AccountManager {
	
    @httpGet
    global static Account getAccount(){
        RestRequest request = RestContext.request;
        String accountId = request.requestURI.substringBetween('/Account/' , '/contacts');
        
        Account result = [SELECT Id, Name, (SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId];
  
        return result;

    }
    
}


Can somebody help me with this?

Thank you,
Fabio

Best Answer chosen by FabioMarcobelli
Naveen ChoudharyNaveen Choudhary
AccountManager Apex Class


@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;
    }      
}

After saving it "Run All" test from developer console. It'll help you to clear the challange.

Mark it as best answer please.

All Answers

FabioMarcobelliFabioMarcobelli

Nevermind, I found the problem.

My URI was wrong, it had "Account" instead of the requested "Accounts".

yuvraj arorayuvraj arora
Challenge Not yet complete... here's what's wrong: 
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.




Showing me this error
Test class
@IsTest
private class AccountManagerTest {

    @isTest static void testGetContact() {
        Id recordId = createTestRecord();
      
      
        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
        List<Account> thisCase = AccountManager.getAccount();
        // Verify results
        System.assert(thisCase != null);
        
    }

  static Id createTestRecord() {
        // Create test record
        Account acc = new Account(
            Name='Test record'
            );
        insert acc;
        return acc.Id;
    }          
}


Main class
@restResource(urlMapping='/Accounts/<Account_ID>/contacts')
global class AccountManager {
    
    @HttpGet
    global static List<Account> getAccount(){
        RestRequest request = RestContext.request;
       String  accountId = request.requestURI.substringBetween('/Accounts/' , '/contacts');
         System.debug('llllllllllllllllllllll'+accountId);
        List<Account> result = [SELECT Id, Name, (SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId];
          System.debug('sssssssssssssss'+result);
        return result;

    }
    
}
Naveen ChoudharyNaveen Choudhary
AccountManager Apex Class


@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;
    }      
}

After saving it "Run All" test from developer console. It'll help you to clear the challange.

Mark it as best answer please.
This was selected as the best answer
Ravi Rao ArrabelliRavi Rao Arrabelli
Thanks Naveen.. It did help me clear the module.
M ParnellM Parnell
This also helped me. Thanks bunches Naveen!
Naveen ChoudharyNaveen Choudhary
Mark this answer as the best answer if it really helped you.
M ParnellM Parnell
I did. If I could mark it twice, I would.
Naveen ChoudharyNaveen Choudhary
Thanks :) 

You can let me know if you have any other problem regarding trailhead badge complition.
david boukhors 23david boukhors 23
Just a remark, I advise you to use:
'/services/apexrest/Accounts/'
 instead of 'https://na1.salesforce.com/services/apexrest/Accounts/' because hard-coding the instance will be problematic when delivering to another environment.
Ivan GnedyshevIvan Gnedyshev

Thanks. Naveen!

Though, I still don't get it why my code coverage was only 85% (return accountTest wasn't covered). I had almost the same code!

P.S. Of course my class was working as I tested it myself with Workbench and Query Editor

Henk PoellHenk Poell
FabioMarcobelli:
Nevermind, I found the problem.
My URI was wrong, it had "Account" instead of the requested "Accounts".

Fabio... You were not alone! Just passed my test with that change too :-)
Venkata Surendra Bezawada 3Venkata Surendra Bezawada 3
I am getting the below error
Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.QueryException: List has no rows for assignment to SObject for  " Create an Apex REST service that returns an account and it's contacts " trailhead task. Please help me where it was wrong.. I have tried different ways,..but unable to finish the challenge....

@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];
        System.debug('acc==>'+acc);
        return acc;
    }
}
----------------------------------------------------------------------------------
@IsTest
private class AccountManagerTest {

    @isTest static void testGetAccount() {
        Id recordId = createTestRecord();
        Test.setMock(HttpCalloutMock.class, new AccountManagerMock());
        RestRequest request = new RestRequest();
        request.requestUri =
            '/services/apexrest/Accounts/'+ recordId +'/Contacts';
        request.httpMethod = 'GET';
        RestContext.request = request;
        Account thisAccount = AccountManager.getAccount();
        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 con = new Contact(
        AccountId = accountTest.Id,
        firstName = 'test',
        lastName  =  'tester');
        insert con;
        return accountTest.Id;
    }          

}
Amir BOUTITIAmir BOUTITI
Just passed my test with the code below:

Main class:
@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;
    }
    
}
Test class:
@isTest
private class AccountManagerTest {

    private static testMethod void getAccountTest() {  
        
        Id recordId = createTestRecord();
        
        // Set up a test request
        RestRequest request = new RestRequest();
        request.requestUri = '/services/apexrest/Accounts/'+recordId+'/contacts' ;
        request.httpMethod = 'GET';
        RestContext.request = request;
        
        // Call the method to test
        Account account = AccountManager.getAccount();
        
        // Verify results
        System.assert(account != null);
    }
    
    static Id createTestRecord() {
        // Create test record
        Account acc = new Account(Name='Test record');
        insert acc;
        return acc.Id;
    }          
}
After saving it, don't forget to "Run All" test from developer console.
Linked2MarkLinked2Mark

Hi - I just set up a new Dev org to do this segment and am having a problem with the url. Any tips?

The base url for the home Sales app is: https://na59.lightning.force.com/one/one.app#/home

I put in the first piece of APEX thats in the segment and used workbench to hit it with the post example but get a 404 error. The uri I am hitting is:

//na59.lightning.force.com/services/apexrest/Cases/