-1

I am getting this on my controller. Can someone please point out the issue:

Error is:

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

ApexClass

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];
    }
}

Here is the VF 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>
4

As a convenience Apex allows you to have a method or variable return value of a single object type - Case in your example - rather than always needing to return a list. A list can handle the case of 0, 1, 2, 3 etc values. But the single object form requires exactly 1 value. In your case you have 0 values, presumably because the cid parameter doesn't correspond to a Case.

Assuming that you want to support the situation where the cid doesn't correspond to a case you can change your methods to e.g.:

public Case getCurrentCase() {
    String cid = ApexPages.currentPage().getParameters().get('cid');
    Case[] cases = [
            SELECT Id, CaseNumber, Reason, Product__c, Expertise__c
            FROM Case
            WHERE Id = :cid
            ];
    return cases.size() == 1 : cases[0] : null;
}
public Case[] getSimilarCases() {
    Case currentCase = getCurrentCase();
    if (currentCase != null) {
        return [SELECT Id, ...];
    } else {
        return new Case[] {};
    }
}

and your page to:

<apex:pageBlock
        title="Similar Cases to {!currentCase.CaseNumber}"
        rendered="{!currentCase != null}"
        >
    ...
0

If a case does not exist with the id value in cid, you will get this exception since you are returning a single SObject. To get around this error, assign your query result to a list of SObjects and check the size before returning the Case if it exists.

-1

I was able to get this working. There were few issues. here is the updated code, may be useful for someone..

displaying similar cases on the console

Apex Class

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];
}

}

VF page

View

I exposed the VF page as a component to be displayed on the left sidebar of the service console and it works good.

  • 1
    This is not an answer, please edit your question to include your final resolution using the answers received as part of the question. Also it would be considered polite if you mark KeithC answer as the correct answer since it appears you used his answer to get it working. – Eric Jul 24 '15 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.