1

I want to display on my case layout other open Cases related to Account from case. I have got a VFP to display them on my Account page layout but I can't display them on my Case layout. Any ideas what to do? I could get those records without VF with trigger on my Cases but i try to find different way to do that.

VFP code:

<apex:page standardController="Account" extensions="contrller">

  <apex:form >
    <apex:pageblock >
       <apex:pageBlockTable value="{!relatedCases}" var="val">
         <apex:facet name="header"><b>Open Customer Service Cases</b></apex:facet>       
         <apex:column value="{!val.CaseNumber}"/>         
         <apex:column value="{!val.Status}"/>
         <apex:column value="{!val.RecordTypeId}"/>

       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>

with controller:

public with sharing class contrller {
Public id Current_Acc_Id;

    public contrller(ApexPages.StandardController controller) {
    Current_Acc_Id = controller.getRecord().id;
    }

    public List<Case> getrelatedCases(){
        List <case> conList = New List<Case>();
        for(Account acc:[select (select status, CaseNumber, RecordTypeId from cases WHERE RecordTypeId = '01220000000QawC' AND Status != 'Closed') from account where id=:Current_Acc_Id]){
           for(case con:acc.cases)
               conList.add(con); 
        }
        return conList;
    }
}

1 Answer 1

2

You can display the Cases related to same account using Case as standard controller.

VFP code:

<apex:page standardController="Case" extensions="CaseExtensionController">
   <apex:form >
    <apex:pageblock >
       <apex:pageBlockTable value="{!relatedCases}" var="val">
         <apex:facet name="header"><b>Open Customer Service Cases {!Case.AccountId}</b></apex:facet>       
         <apex:column value="{!val.CaseNumber}"/>         
         <apex:column value="{!val.Status}"/>
         <apex:column value="{!val.RecordTypeId}"/>

       </apex:pageBlockTable> 
    </apex:pageblock>
  </apex:form>
</apex:page>

Controller:

public with sharing class CaseExtensionController{
    public Id Current_Acc_Id {get;set;}

    public contrller(ApexPages.StandardController controller) {
        Case caseObject = (Case)controller.getRecord();
        Current_Acc_Id = caseObject.AccountId;
    }

    public List<Case> getrelatedCases(){
        List <case> conList = new List<Case>();
        for(Case relatedCase: [SELECT Status, CaseNumber, RecordTypeId FROM Case WHERE RecordTypeId = '01220000000QawC' AND Status != 'Closed' AND AccountId =:Current_Acc_Id]){
            conList.add(relatedCase); 
        }
        return conList;
    }
}

Add this Visual force page in the case layout.

6
  • Does account have an Id? I think it's object and holds information about companies. The User object is 'User' and holds information about the Salesforce users, so User has an ID. Am i right? There is error with this code "Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Case.AccountId
    – Jacob K
    Commented Jul 11, 2017 at 9:22
  • Every object in Salesforce has Id. Yes, account holds information about companies.
    – Ankita
    Commented Jul 11, 2017 at 9:23
  • Actually i'm new with apex, could u explain how to query acc.id ?
    – Jacob K
    Commented Jul 11, 2017 at 9:48
  • AccountId field is not being used in the VF page and it was used in controller. Therefore, that error occurred. Refer developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – Ankita
    Commented Jul 11, 2017 at 9:49
  • You can use SOQL to query the account Id field or simply use AccountId in the VF page to resolve the issue. I have updated the VF page code snippet.
    – Ankita
    Commented Jul 11, 2017 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .