You need to sign in to do that
Don't have an account?
FabioMarcobelli
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
@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
Nevermind, I found the problem.
My URI was wrong, it had "Account" instead of the requested "Accounts".
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;
}
}
@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.
You can let me know if you have any other problem regarding trailhead badge complition.
'/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.
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
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 :-)
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;
}
}
Main class: Test class: After saving it, don't forget to "Run All" test from developer console.
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/
@RestResource(urlMapping='/Accounts/*/contacts/')
global with sharing class AccountManager {
@HttpGet
global static Account getAccount() {
RestRequest request = RestContext.request;
// grab the AccountId from the mid of the URL
String accountId = request.requestURI.substringBetween('Accounts/', '/contacts');
Account result = [SELECT Id,Name, (select Id,Name from Contacts)
FROM Account
WHERE Id = :accountId];
return result;
}
}
--------------------------------------------------------------------------------------------
@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;
}
}
Its very useful for me to completing my challenge.......!!