+ Start a Discussion
SalesforceLearnerSalesforceLearner 

Display case information using Lightning component

I want to display cases related to loggedin user account. It is displaying empty list(it is not returning any values). When tried in query editor the query is working I am not understanding where i am making mistake can anyone pls help me with this ?
here is my code:
Controller:
public class CaseInformation{

        @AuraEnabled
        public static List<Case> getCases() {

        ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
        if(contactId == null)
        {
            return NULL;            
        }
        ID AccID  = [Select AccountID from Contact where id =: contactid ].AccountId;
            if(AccID == null){
                return NULL;
            }
            System.debug('AccountId :' + AccID + 'ContactId:' + contactId);
            List<Account> allAssocaiatedAccounts = [Select Id, name, industry from Account where Id =: AccID];
            if(allAssocaiatedAccounts.size() != 0){
            return [Select CaseNumber,Status,Priority,Type,Subject,AccountID from Case where AccountID IN: allAssocaiatedAccounts];
            }
            else {
                return NULL;
            }
    }  

}
Lightning Component:
 
<aura:component controller="CaseInformation">
<aura:attribute name="caseRows" type="Object[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:iteration var="cas" items="{!v.caseRows.Cases}">
                    <tr class="border_bottom">
                        <td > {!cas.CaseNumber} </td>
                        <td > {!cas.Subject}  </td>
                        <td >{!cas.Status}</td>
                        <td> <ui:outputDate format="MMM dd yyyy" value="{!cas.CreatedDate}"/> </td>
                        <td >{!cas.Account.Name}</td>
                    </tr><hr class="hrcolclass" />
                </aura:iteration>
</aura:component>

Component Controller:
 
({
    doInit : function(component, event, helper){
        helper.getAllCases(component);
     }
})
Helper:
({
    getAllCases : function(component, event, helper) {
        var action = component.get("c.getCases");

        action.setCallback(this, function(a){
            component.set("v.caseRows", a.getReturnValue());
        });
        $A.enqueueAction(action);
    }
})



 
Basant Kr VermaBasant Kr Verma
Hi Try chnaging component code to bellow, heighligted changes in bold

<aura:component controller="CaseInformation">
<aura:attribute name="caseRows" type="Case[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:iteration var="cas" items="{!v.caseRows}">
                    <tr class="border_bottom">
                        <td > {!cas.CaseNumber} </td>
                        <td > {!cas.Subject}  </td>
                        <td >{!cas.Status}</td>
                        <td> <ui:outputDate format="MMM dd yyyy" value="{!cas.CreatedDate}"/> </td>
                        <td >{!cas.Account.Name}</td>
                    </tr><hr class="hrcolclass" />
                </aura:iteration>
</aura:component>
Jai ChaturvediJai Chaturvedi
Hi,

Use this:

<aura:attribute name="caseRows" type="List[]"/>

I think this will work smoothly. Mark this as answer if works.


Cheers
 
David Roberts 4David Roberts 4
According to https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_attr_types_collection.htm
you can use either
<aura:attribute name="caseRows" type="List"/>
or
<aura:attribute name="caseRows" type="String[]"/>
Also, the get method simplifies to
public static List<Case> getCases() {
        Id userId = Userinfo.getUserid();
        return [Select CaseNumber,Status,Priority,Type,Subject from Case where ownerID = :userId]; 
}//getCases

Sorry if this is two years too late but hopefully it will help others.
 
David Roberts 4David Roberts 4
you'll also need to add Case fields to the select statement if they are wanted in the output
Select CaseNumber,Status,Priority,Type,Subject,Account.Name, CreatedDate from Case
Nirdesh_BhattNirdesh_Bhatt
How to do using LWC? 
Nirdesh_BhattNirdesh_Bhatt
.html

<template>
  <h6> Showing Cases based on Account Selected on this Value Plan</h6>
  <lightning-datatable data={data} columns={columns} key-field="Id">
  </lightning-datatable>
</template>
--------------------------------------------------------
.js
import { LightningElement ,api,wire,track} from 'lwc';
import getAllCase from '@salesforce/apex/GetAllCases.getAllCase';


export default class DatatableEx12 extends LightningElement {
    @track columns = [{
            label: 'Case Number',
            fieldName: 'CaseNumber',
            type: 'Auto Number',
            sortable: true
        },
        {
            label: 'Subject',
            fieldName: 'Subject',
            type: 'Text(255)'
        }, {
            label: 'Status',
            fieldName: 'Status',
            type: 'Picklist'
        }, {
            label: 'Case Type',
            fieldName: 'Type',
            type: 'Picklist'
        }
    ];
    @track error;
    @track data ;
    @api recordId;
    @wire(getAllCase)
    wiredOpps({error,data}) {
        if (data) {
            this.data = data;
            console.log(data);
            console.log(JSON.stringify(data, null, '\t'));
        } else if (error) {
            this.error = error;
        }
    }
    
    
}
--------------------------------------------------------
public with sharing class GetAllCases {   

    @AuraEnabled(cacheable=true)
    public static List<Case> getAllCase(String accountId) {
    this.acnt= (Account)controller.getRecord();

     Account acc = [Select id FROM Account where id = :acnt.id];
}
}

Can anyone help to fetch cases based on account selection? 
David Roberts 4David Roberts 4
@AuraEnabled(cacheable=true) public static List getAllCases(String accId) { List lstCases = [SELECT id, CaseNumber FROM case where accountId = : accId]; //add fields to select as required return lstCases; }// getAllCase Should do the trick.
Nirdesh_BhattNirdesh_Bhatt
Thanks for the quick response David but still I faced the same issue can you please tell me anything missing in my LWC? 
Nirdesh_BhattNirdesh_Bhatt
It displays all cases not particular that account's cases;
can you please help with this. it really urgent for me. 
Thank you so much for your help! 
David Roberts 4David Roberts 4
I'm afraid I'm not veresed in LWC.
But it looks like @wire(getAllCase) should be plural as in @wire(getAllCases)
and the function need to change to what I sent before.
 
@AuraEnabled
public static List getAllCases(String accId) {
   List lstCases = [SELECT id, CaseNumber FROM case where accountId = : accId];
   //add fields to select as required
   return lstCases;
}// getAllCase

Hope this helps.
Nirdesh_BhattNirdesh_Bhatt
My updated method is as below 
@AuraEnabled(cacheable=true) 
     public static List<Case> getAllCases(String accountId)
{ 
List<Case> lstCases = [SELECT Id, AccountId, CaseNumber,Subject,Status,Type FROM Case where accountId = : accountId]; //add fields to select as required 
return lstCases;
 }

 
David Roberts 4David Roberts 4
Looks good. Sorry I left <Case> out!
I would change the parameter name passed in to avoid confusion. e.g. (String accId).
then "WHERE AccountId =: accId" is clearer.
Have you tried adding diagnostics to see what is listed.
system.debug('accountId in getAllCases = '+accId);
system.debug('lstCases...');
system.debug(lstCases);


 
Nirdesh_BhattNirdesh_Bhatt
Hi David,
Yes; I have check that diagnostics 

system.debug('accountId in getAllCases = '+accId);  // Didn't get aacid = null
system.debug('lstCases...'); // null 
system.debug(lstCases); // list of all cases display not particular account 
 
David Roberts 4David Roberts 4
You need to pass in the account Id.
As I say, I'm not familiar with LWC but it'll be structured something like this example:
@wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_NAME_FIELD] })
    record;

 
David Roberts 4David Roberts 4
I guess:
@wire(getAllCases, { accId: '$recordId' })
    cases;

 
Nirdesh_BhattNirdesh_Bhatt
Hi david,
Thanks for the help it's not working  i have posted my updated logic if possible can you please let me know what should be the issue? 
.html

import { LightningElement ,api,wire,track} from 'lwc';
import getCases from '@salesforce/apex/GetAllCases.getCases';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import ACC_ID from '@salesforce/schema/Case.AccountId';
var accountId;


export default class DatatableEx12 extends LightningElement {
    @track columns = [{
            label: 'Case Number',
            fieldName: 'CaseNumber',
            type: 'Auto Number',
            sortable: true
        },
        {
            label: 'Subject',
            fieldName: 'Subject',
            type: 'Text(255)'
        }, {
            label: 'Status',
            fieldName: 'Status',
            type: 'Picklist'
        }, {
            label: 'Case Type',
            fieldName: 'Type',
            type: 'Picklist'
        }
    ];
    @track error;
    @track data ;
    @api recordId;
    @api accountId;

    @api Case;

    @track accId;
    accountId;

    @wire(getCases, {accountId: '$recordId'})
    //cases;
    wiredOpps({error,data}) {
        if (data) {
            this.accId = getFieldValue(data, ACC_ID);
            this.data = data;
            console.log(data);
            console.log(JSON.stringify(data, null, '\t'));
            accountId = this.accId;
        } else if (error) {
            this.error = error;
        }
    }
    
    
}



======================================
 @AuraEnabled(cacheable=true) 
     public static List<Case> getCases(String accountId)
{ 
system.debug('before accountId in getAllCases: = '+ accountId);

List<Case> lstCases = [SELECT Id, AccountId, CaseNumber,Subject,Status,Type FROM Case where AccountId =: accountId]; //add fields to select as required 
system.debug('after accountId in getAllCases: = '+ accountId);
system.debug('lstCases...');
system.debug(lstCases);

return lstCases;