function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Darshit Pathak 3Darshit 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?

Vivian Charlie 1208Vivian Charlie 1208

Hi Darshit,

 

I assume you have a query as follows

Account objA = [Select Id, name from Account ..... limit 1];
 

Please change it to

Account objA;
list<Account> lstA = [Select Id, name from Account ..... limit 1];

if(lstA!= null && !lstA.isEmpty()){

objA = lstA[0];

}
 

 

If the query result does not return any value and is directly assigned to any object this error occurs

 

Thanks

Vivian

Darshit Pathak 3Darshit Pathak 3

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.

Darshit Pathak 3Darshit Pathak 3
This is my Test Class

@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);
    
    }
}
Vivian Charlie 1208Vivian Charlie 1208

Hi Darshit,

 

You would have received the attempt to de-reference a null object at this line

return acc[0];


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

Darshit Pathak 3Darshit Pathak 3

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.

 

Vivian Charlie 1208Vivian Charlie 1208

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

Darshit Pathak 3Darshit Pathak 3

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?

Vivian Charlie 1208Vivian Charlie 1208

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

@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];
        
		If(acc!=null && !acc.isEmpty()){
			account = acc[0];
		}
        return account;
    }
}


Thanks

Vivian

Darshit Pathak 3Darshit Pathak 3
Problem remains the same.

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.
Vivian Charlie 1208Vivian Charlie 1208

Darshit,

 

Just to verify do you have multiple classes where the url mapping is same? i.e

@RestResource(urlMapping='/Account/*')

If Yes. please delete the unnecessary class

 

You do not need the typecasting statement i.e.

Id accI=(Id)accountId;
 

You have not performed a null check at the following line

account=acc[0];

 

Can you try the following code once

@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 from Account where Id=:accountId];
        
		If(acc!=null && !acc.isEmpty()){
			account = acc[0];
		}
        return account;
    }
}


Thanks

Vivian

Darshit Pathak 3Darshit Pathak 3

I tried this code but that same index out of bounds exception.

and no. there is no other with the same mapping.

Vivian Charlie 1208Vivian Charlie 1208
Darshit, If you are receiving the same index out of bounds exception then its definitely not at the acc[0] line. I believe we will have to investigate the following line *accountId=request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);*
Darshit Pathak 3Darshit Pathak 3

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.

Darshit Pathak 3Darshit Pathak 3

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?

Vivian Charlie 1208Vivian Charlie 1208

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

Darshit Pathak 3Darshit Pathak 3

thank you Vivian for supporting.

main issue was of /Accounts/<Account_ID>/contacts  only.

It is resolved by this post.

Thank You.

Vivian Charlie 1208Vivian Charlie 1208

Awesome,

 

Feel free to mark this as resolved so that others can benefit from this post.

 

Thanks

Vivian