+ Start a Discussion
Abhiram Sheshadri 9Abhiram Sheshadri 9 

Apex Web Services Challenge throwing System.QueryException: List has no rows for assignment to SObject

Hi,

For the Apex web services challenge, I have written the following class

@RestResource(urlMapping='/Accounts/*/contacts')
global with sharing class AccountManager {
     @HttpGet
    global static Account getAccount(){
        RestRequest request = RestContext.request;
        String Accountid = request.requestURI.substring(10,25);
        Account result = [SELECT id,name ,(SELECT id,name from contacts) from Account where id=:Accountid];
        return result;
    }
}

When I test this with passing the account ID in Workbench REST Explorer I am able to fetch both the account details as well as it's associated contact details.

But when I check the challenge I get the below error.

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

Any Help?

Thanks & Regards,
Abhiram Sheshadri
Best Answer chosen by Abhiram Sheshadri 9
Abhiram Sheshadri 9Abhiram Sheshadri 9
String Accountid = request.requestURI.substring(10,25);

The issue was with above line which was not resolving the URI, in turn not fetching any data in the query.

Changed the above code to below and it worked.
 
String Accountid = request.requestURI.substringBetween('Accounts/','/contacts');

 

All Answers

Abhiram Sheshadri 9Abhiram Sheshadri 9
String Accountid = request.requestURI.substring(10,25);

The issue was with above line which was not resolving the URI, in turn not fetching any data in the query.

Changed the above code to below and it worked.
 
String Accountid = request.requestURI.substringBetween('Accounts/','/contacts');

 
This was selected as the best answer
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.