You need to sign in to do that
Don't have an account?
Siddharth . 2
Hi,I was solving a trailhead challenge for Apex Web Services.I am unable to pass the challenge as the error shows is below.
Error:System.QueryException: List has no rows for assignment to SObject
Stack Trace:
Class.AccountManager.getAccount: line 10, column 1
Class.AccountManagerTest.TestGetById: line 19, column 1
Account ManagerClass:
@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 accountdetails = [Select Id,name,(Select Id,Name from Contacts) from Account where Id=:AccountId LIMIT 1];
return accountdetails;
}
}
AccountManagerTest:
@isTest
public class AccountManagerTest {
@isTest static void TestGetById(){
Account testaccount = new Account(name='TestAccount');
insert testaccount;
Contact objContact = new Contact(LastName ='TestContact',AccountId=testaccount.Id);
insert objContact;
id recordid=testaccount.Id;
Restrequest request=new Restrequest();
request.requestURI='https://ap8.lightning.force.com/services/apexrest/Account/'+recordid+'/contact';
request.httpMethod='GET';
RestContext.request=request;
Account thisAccount=AccountManager.getAccount();
//Verify the results
System.assert(thisAccount!= null);
System.assertEquals('TestAccount', thisAccount.Name);
}
}
Stack Trace:
Class.AccountManager.getAccount: line 10, column 1
Class.AccountManagerTest.TestGetById: line 19, column 1
Account ManagerClass:
@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 accountdetails = [Select Id,name,(Select Id,Name from Contacts) from Account where Id=:AccountId LIMIT 1];
return accountdetails;
}
}
AccountManagerTest:
@isTest
public class AccountManagerTest {
@isTest static void TestGetById(){
Account testaccount = new Account(name='TestAccount');
insert testaccount;
Contact objContact = new Contact(LastName ='TestContact',AccountId=testaccount.Id);
insert objContact;
id recordid=testaccount.Id;
Restrequest request=new Restrequest();
request.requestURI='https://ap8.lightning.force.com/services/apexrest/Account/'+recordid+'/contact';
request.httpMethod='GET';
RestContext.request=request;
Account thisAccount=AccountManager.getAccount();
//Verify the results
System.assert(thisAccount!= null);
System.assertEquals('TestAccount', thisAccount.Name);
}
}
Greetings to you!
There is a problem in URI, it is not resolving the URI. Changed the above code to below and it will work.
AccountManager Class:
AccountManagerTest Class:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Thank you for sorting this out.Could you please let me know why do I have to create a method to create a new record is the test class.Why I should have not used the Id generated by creating the object as I did in my first approach.
Eagerly waiting for your answer.
Happy Learning!
I have tried the code provided by you but it still give the same error.