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
Steve FSteve F 

List has no rows for assignment

Hi guys, I am new to APEX, and I am trying to create a class to access via REST.
Please let me know what I am doing wrong here:
 
@isTest(SeeAllData = true)
@RestResource(urlMapping='/v1/cases/*')
global with sharing class REST_getCases_V1 {
    
    @HttpGet
    global static Case doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;        
        String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);        
        Case result = [select CaseNumber, status from case where CaseNumber = :caseId]; <<---Line 14
        return result;
    }
}
error:
System.QueryException: List has no rows for assignment to SObject Class.REST_getCases_V1.doGet: line 14, column 1
User-added image

I am going by this sample:
https://youtu.be/C64pLdXmEoo

Any help is appreciated,

Thank you.

Steve



 
Best Answer chosen by Steve F
Amit Chaudhary 8Amit Chaudhary 8
TRy below code
 
@RestResource(urlMapping='/v1/cases/*')
global with sharing class REST_getCases_V1 
{
    @HttpGet
    global static List<Case> doGet() 
    {
        Case result;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;        
        String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
                
        List<Case> ListCase = [select CaseNumber, status from case where CaseNumber = :caseId];
        return ListCase;
    }
}

Let us know if this will help u

All Answers

Steve FSteve F
Btw, I made sure that the record exist:
<records xsi:type="sf:Case">
               <sf:Id xsi:nil="true"/>
               <sf:CaseNumber>00013157</sf:CaseNumber>
               <sf:Status>In Progress</sf:Status>
</records>

 
doravmondoravmon
You can follow this steps to check the error:
First, does caseId have a value?
Second, use Case[] result.
Third...what's line14?
Naval Sharma4Naval Sharma4
Hi Steve,

This error comes when you query doesn't return any rows.

Updated your code.
@isTest(SeeAllData = true)
@RestResource(urlMapping='/v1/cases/*')
global with sharing class REST_getCases_V1 {
    
    @HttpGet
    global static Case doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;        
        String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);        
        Case result; 
        for(Case c : [select CaseNumber, status from case where CaseNumber = :caseId]){
            result = c;
        }
        return result;
    }
}

 
doravmondoravmon
Using queries in for loop is not a good idea.... Suggest to use Case[] result instead.
Amit Chaudhary 8Amit Chaudhary 8
Hi Steve F,

1) No need to seeAllData here
2) Make sure will pass valid case number
3) Update your code like below
@RestResource(urlMapping='/v1/cases/*')
global with sharing class REST_getCases_V1 
{
    @HttpGet
    global static Case doGet() 
    {
        Case result;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;        
        String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
                
        List<Case> ListCase = [select CaseNumber, status from case where CaseNumber = :caseId];
        if( ListCase.size()>0 )
        {
            result = ListCase[0];
        }
        return result;
    }
}

I tested above code in my developer org which is working fine

User-added image
NOTE:- please pass the valid case number for result

Let us know if this will help you
Steve FSteve F
Thank you so very much for all your help here. What do I need to do to get more then one record back?
Just for testing I modified the query and removed the where but I still only get one record back.
Amit Chaudhary 8Amit Chaudhary 8
TRy below code
 
@RestResource(urlMapping='/v1/cases/*')
global with sharing class REST_getCases_V1 
{
    @HttpGet
    global static List<Case> doGet() 
    {
        Case result;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;        
        String caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
                
        List<Case> ListCase = [select CaseNumber, status from case where CaseNumber = :caseId];
        return ListCase;
    }
}

Let us know if this will help u
This was selected as the best answer
Steve FarmerSteve Farmer
Hi guys, thanks again for getting me started with that. 

So how do I access this from the outside? Like using postman? How do I authenticate to access my class? Do I need to write a new class to login? Do you have any samples?

Steve