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
RAJ PADMANABHAN 8RAJ PADMANABHAN 8 

System.QueryException: List has no rows for assignment to SObject - Display simiar Cases

I am getting following error when the code is deployed:  My objective was to display similar cases on the lefside bar of the service console using custom VF page

caused by: System.QueryException: List has no rows for assignment to SObject

Class.SimilarCasesController.getCurrentCase: line 4, column 1
Class.SimilarCasesController.getSimilarCases: line 7, column 1


Here is the controller I created:
public class SimilarCasesController {
public Case getCurrentCase() {
    String cid = ApexPages.currentPage().getParameters().get('cid');
    return [SELECT Id, CaseNumber, Reason, Product__c, Expertise__c FROM Case WHERE Id = :cid];
    }
public Case[] getSimilarCases() {
Case currentCase = getCurrentCase();
return [SELECT Id, CaseNumber, Owner.Name, Subject, Status FROM Case WHERE Product__c = :currentCase.Product__c AND Expertise__c = :currentCase.Expertise__c AND Reason = :currentCase.Reason AND Id != :currentCase.Id];
    }
}
 and here is the Visfualforce page

<apex:page controller="SimilarCasesController" tabStyle="Case">
    <apex:form >
       <apex:sectionHeader title="Similar Cases" subtitle="{!currentCase.CaseNumber}"/>
       <apex:pageBlock title="Similar Cases to {!currentCase.CaseNumber}">
        <apex:pageBlockTable value="{!similarCases}" var="case">
            <apex:column >
                <apex:outputLink value="{!URLFOR($Action.Case.View, case.Id)}">View</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Number" value="{!case.CaseNumber}"/>
            <apex:column headerValue="Owner" value="{!case.Owner.Name}"/>
            <apex:column headerValue="Subject" value="{!case.Subject}"/>
            <apex:column headerValue="Status" value="{!case.Status}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
     </apex:form>
</apex:page>
 
Best Answer chosen by RAJ PADMANABHAN 8
Shashikant SharmaShashikant Sharma
The reason for it is that cid is not able to find a case from the query in method getCurrentCase

Change your method to:
 
public Case getCurrentCase() {
    String cid = ApexPages.currentPage().getParameters().get('cid');
    List<Case> cases = [SELECT Id, CaseNumber, Reason, Product__c, Expertise__c FROM Case WHERE Id = :cid];
 
   return cases.size() > 0 ? cases.get(0) : new Case(); 
    }

It will return a new instance of case in case Id is not finding the case record from Query.

All Answers

Shashikant SharmaShashikant Sharma
The reason for it is that cid is not able to find a case from the query in method getCurrentCase

Change your method to:
 
public Case getCurrentCase() {
    String cid = ApexPages.currentPage().getParameters().get('cid');
    List<Case> cases = [SELECT Id, CaseNumber, Reason, Product__c, Expertise__c FROM Case WHERE Id = :cid];
 
   return cases.size() > 0 ? cases.get(0) : new Case(); 
    }

It will return a new instance of case in case Id is not finding the case record from Query.
This was selected as the best answer
RAJ PADMANABHAN 8RAJ PADMANABHAN 8
Shashikant.. The change you suggested works awesome.  Thank you
Shashikant SharmaShashikant Sharma
Glad that it helped you :) .
RAJ PADMANABHAN 8RAJ PADMANABHAN 8
I modified the code to use standard controller.  and works as expected. Here is the final if someone is interested in displaying similar cases

public class SimilarKBCasesExtension {

    public Case currentRecord{get; set;}
    
    public SimilarKBCasesExtension (ApexPages.StandardController controller)
     {
        currentRecord = [SELECT Id, Expertise__c,Product__c,Reason FROM Case WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
  
    public Case[] getSimilarCases()
    {
    
     //   Case currentCase = currentRecord();
        return [SELECT CaseNumber, Status,Product__C,Expertise__C,Reason FROM Case WHERE 
            Product__c = :currentRecord.Product__c AND
            Expertise__c = :currentRecord.Expertise__c AND
            Reason = :currentRecord.Reason AND
            Id != :currentRecord.Id];
    }
   
 }
 
 <apex:page standardController="Case" extensions="SimilarKBCasesExtension">
    <apex:form >
       <apex:sectionHeader title="Similar Cases" subtitle="{!Case.CaseNumber}"/>
       <apex:pageBlock title="Similar Cases to {!Case.CaseNumber}">
        <apex:pageBlockTable value="{!similarCases}" var="case">
            <apex:column >
                <apex:outputLink value="{!URLFOR($Action.Case.View, case.Id)}">View</apex:outputLink>
            </apex:column>
            <apex:column headerValue="Case Number" value="{!Case.CaseNumber}"/>
            <apex:column headerValue="Product" value="{!Case.Product__c}"/>
            <apex:column headerValue="Expertise" value="{!Case.Expertise__c}"/>
            <apex:column headerValue="Reason" value="{!Case.Reason}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
     </apex:form>
</apex:page>