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
Stephen Judd - DevStephen Judd - Dev 

Apex REST service - List Index out of bounds

In the Trailhead Apex Web Services Unit, my class has 100% test coverage and using the Workbench: REST Explorer, responds correctly to existing accounts with or without associated contacts. If an invalid Id is passed in, I return an empty account.

However, when I Check the challenge, I get "System.ListException: List index out of bounds: 0" problem in response. I'm not sure what's wrong - any pointers?
Best Answer chosen by Stephen Judd - Dev
Abhiram Sheshadri 9Abhiram Sheshadri 9
Hi Stephen,

I executed your code in my org. I think the issue is with the below line of code

 
String aId = request.requestURI.substring(request.requestURI.lastIndexOf('Account/')+8,
                      request.requestURI.lastIndexOf('/'));
You can change the above line to the below :
String aId = request.requestURI.substringBetween('Accounts/','/contacts');

I think that would work. Please let me know if you find any further issues

Thanks,
Abhiram Sheshadri

 

All Answers

Jeff DouglasJeff Douglas
Stephen,

What's the URL of this unit so I can take a look at the challenges. Can you also post your code?

Thanks
Jeff Douglas
Trailhead Developer Advocate
Stephen Judd - DevStephen Judd - Dev
The unit is: https://developer.salesforce.com/trailhead/apex_integration_services/apex_integration_webservices

My code:
 
@RestResource(urlMapping='/Account/*/contacts')
global with sharing class AccountManager {
    @HttpGet
    global static Account getAccount() {
        RestRequest request = RestContext.request;
        String aId = request.requestURI.substring(
            request.requestURI.lastIndexOf('Account/')+8,
            request.requestURI.lastIndexOf('/'));
        //String aId = '001i000001hu4Dj'; //For testing in anonymous execution window
        Account[] result = [SELECT Id,Name,(select id,Name from contacts)
                         FROM Account
                       WHERE Id = :aId];
        System.debug(result);
        if(!result.isEmpty()){
           return result[0]; 
        } else{
            Account a = new Account();
            return a;
        }              
    }  
}

Thanks for taking a look. I originally tried getting the account directly, but it would return an arror if there were no associated contacts, so I used this way.
Abhiram Sheshadri 9Abhiram Sheshadri 9
Hi Stephen,

I executed your code in my org. I think the issue is with the below line of code

 
String aId = request.requestURI.substring(request.requestURI.lastIndexOf('Account/')+8,
                      request.requestURI.lastIndexOf('/'));
You can change the above line to the below :
String aId = request.requestURI.substringBetween('Accounts/','/contacts');

I think that would work. Please let me know if you find any further issues

Thanks,
Abhiram Sheshadri

 
This was selected as the best answer
Stephen Judd - DevStephen Judd - Dev
Thanks Abhiram - I was looking in all the wrong places!