You need to sign in to do that
Don't have an account?
RAJ 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>
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>
Change your method to:
It will return a new instance of case in case Id is not finding the case record from Query.
All Answers
Change your method to:
It will return a new instance of case in case Id is not finding the case record from Query.
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>