You need to sign in to do that
Don't have an account?
Darshit Pathak 3
code running in developer console but error on trailhead.It's showing "List has No rows for Assignment to SObject". But the code passes tests with 100% coverage on developer console.
Unit is Developer Intermediate ->Apex Integration Services -> Apex Web Services
If this is the actual error how is it possible that code runs perfectly in developer console?
Hi Darshit,
I assume you have a query as follows
Please change it to
If the query result does not return any value and is directly assigned to any object this error occurs
Thanks
Vivian
Thanks for the reply
This is my code.
When I tried this trailhead is showing following error:
System.NullPointerException: Attempt to de-reference a null object
@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager {
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
String accId=request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
System.debug(accId);
List<Account> acc=[select id,name from Account where Id=:accId];
return acc[0];
}
}
But while I test in developer console test case is passed with 100% code coverage.
@isTest
global class AccountManagerTest {
@isTest
static void testAccMngr(){
RestRequest request = new RestRequest();
Account acc=new Account(Name='Hello');
insert acc;
request.requestUri ='https://ap5.salesforce.com/services/apexrest/Account/'+acc.Id;
System.debug(request.requestUri);
request.httpMethod = 'GET';
RestContext.request = request;
// Call the method to test
Test.startTest();
Account thisAccount = AccountManager.getAccount();
Test.stopTest();
System.assert(thisAccount != null);
System.assertEquals('Hello', thisAccount.name);
}
}
Hi Darshit,
You would have received the attempt to de-reference a null object at this line
Assume your query did not return any result the list acc would be null. Since acc is null you receive an error when you try to access any element from the list.
Inorder to investigate the issue you can setup debugs to understand what value comes at every step.
Thanks
Vivian
How can I check logs of trailhead???
because whenever I run this code in my developer console it works perfectly and all System.debug() prints the expected results.
I have put System.debug(request.requestUri); in test class and
System.debug(accId); //to print recived Id
Both has the same ID.
while testing in developer console it passes the test with 100% code coverage.
Darshit,
For the sake of clarification can you add necesary null checks to the code and then try the TrailHead verification. There is a very high possibility that when TrailHead runs the checks they maybe running positive as well as negative test cases to check any vulnerabilities in the code. You test class only checks for a positive use case where the Id value is passed and does not handle a negative case when the id is not available.
Let us first concentrate on rectifying the code once that is done, let us verify the results.
Thanks
Vivian
I changed the code in AccoutManager.apx saved and checked on trailhead.
@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager {
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
String accountId=request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
System.debug(accountId);
Account account=new Account(Name='Hello');
List<Account> acc=[select id,name,(select id,name from Contacts) from Account where Id=:accountId];
// System.debug(acc[0].name);
if(acc!=null && !acc.isEmpty())
account=acc[0];
return account;
}
}
But still the errror is same
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.ListException: List index out of bounds: 0
Now how can this error come?
if the list is empty account=acc[0] will not be executed then how can it be out of bounds Exception?
Darshit,
1st things first if there is any exception BELIEVE me something IS WRONG in the code. Machines do not tend to make mistakes, humans do.
Now coming back to your code please update the code as follows
Thanks
Vivian
then I changed code to this:
@RestResource(urlMapping='/Account/*')
global with sharing class AccountManager {
@HttpGet
global static Account getAccount(){
RestRequest request = RestContext.request;
Account account=new Account(Name='Hello');
String accountId='';
accountId=request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
Id accI=(Id)accountId;
// System.debug(accountId);
if(accI!=null){
List<Account> acc=[select id,name from Account where Id=:accI];
// System.debug(acc[0].name);
account=acc[0];
}
return account;
}
}
So the error has been changed now on trailhead.
There was an unexpected error in your org which is preventing this assessment check from completing: System.StringException:
Invalid id: contacts
what is id:contacts in this code.. I don't know.
Darshit,
Just to verify do you have multiple classes where the url mapping is same? i.e
If Yes. please delete the unnecessary class
You do not need the typecasting statement i.e.
You have not performed a null check at the following line
Can you try the following code once
Thanks
Vivian
I tried this code but that same index out of bounds exception.
and no. there is no other with the same mapping.
It prints the correct Id in System.debug()
which I pass through the test class.
When I insert a dummy Account object and append it's Id to this Url it is recieved by the AccountManager class while testing through developer console.
But there is something going wrong while trailhead checks.
Since test class don't have access to org's data without (seeAllData=true) I can't append existing org's account's Id.
It's also written on trailhead that
To pass this challenge, create an Apex REST class that is accessible at '/Accounts/<Account_ID>/contacts'.
So how can I pass Account Id in UrlMapping?
This may be a problem.
If this is resolved. then It might resolve all other problems.
Can You help on this?
Darshit,
There already exists a post for this on the community with the solution. You can find it it here (https://developer.salesforce.com/forums/?id=906F0000000MJmHIAW).
Thanks
Vivian
thank you Vivian for supporting.
main issue was of /Accounts/<Account_ID>/contacts only.
It is resolved by this post.
Thank You.
Awesome,
Feel free to mark this as resolved so that others can benefit from this post.
Thanks
Vivian