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
SFDC New learnerSFDC New learner 

Need help on lightning component

Hi All,

I am trying to get all the cases and related email that is recently sent to the customer.
 I am able to display all the casenumbers, but not the last email sent. 
Below is my code. 

public class CaseRelatedListApex {
    
    @AuraEnabled
    public static List<Case> getObject(Id  sessId){
        LIST< session__c > objsessList = new LIST< session__c >();
        LIST< Case > objCaseList = new LIST< Case >();
      
        
             if(sessId!=null)
        {
           objCaseList =[select id,session_ID__c,CaseNumber from case where  session_ID__c =:sessId  limit 4];
          
        }
         return  objCaseList;
        }
       @AuraEnabled
    public Static List<Datetime> getmessagedt(List<Case> objCaseList){
     // Case  objCase = new Case (); 
     List<DateTime> dt = new List<DateTime>();
        if(objCaseList.size()>0)
        {
            for(Case objCase :[select id,session_ID__c,CaseNumber,(select id,MessageDate from EmailMessages where incoming = False order by MessageDate desc limit 1) MessageDt from case where ID = :objCaseList ]){
                for(EmailMessage e : objCase.EmailMessages){
                    dt.add(e.MessageDate);
                }
            }
           
        } 
        return  dt;
    }
   
}
Component:
<aura:component controller = "CaseRelatedListApex" implements="force:appHostable,flexipage:availableForAllPageTypes,home:availableForDesktop,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction">
    <aura:attribute name="sessId" type="String"/>
    <aura:attribute name="cases" type="Case[]"/>
    <aura:attribute name="caseId" type="Id"/>
    <aura:attribute name ="cas" type="Case"/>
    <aura:attribute name="LastEmail" type="Datetime"/>
    <aura:handler name="init" value="{!this}" action="{!c.check}" />
    <aura:attribute name="totalCases" type="String"/>
   

    
    <form class="slds-form_horizontal" role ="list">
        <article class="slds-card">
                            <div class="slds-card__header slds-grid" >
                                <header class="slds-media slds-media--center slds-has-flexi-truncate">
                                    <div class="slds-media__figure">
                                        <lightning:icon iconName="standard:case" size ="small" alternativeText="Case" />
                                    </div>
                                    <div class="slds-media__body slds-truncate">
                                        <h2> <b> <span class="slds-text-heading--small">Cases ({!v.totalCases})   </span> </b></h2>
                                    </div>
                                </header>
                                 </div>
       <table  style ="margin-left :10px;">
           <thead>
               <tr >
                   <th scope="col" style ="font-weight:normal" >
                           <h1> <span >Case Number:</span></h1>
                    </th>
                    <th scope="col" style ="font-weight:normal">
                        <h1> <span >Last Email:</span></h1>
                    </th>
                    
               </tr>
           </thead><br/>
            <tbody>
                <aura:iteration items="{!v.cases}" var="ocas">
                  <tr class="slds-text-title" style ="font-size: .8125rem" >                  
                    <!--<td style =" color: rgb(22, 50, 92); text-decoration: dotted underline;">-->
                      <td>
                        <a href="#"  data-recId="{!ocas.Id}"  onclick="{!c.handleClick}">{!ocas.CaseNumber}</a>
                      </td>
                    <!--</td>-->
                    <td>
                        {!v.LastEmail}
                    </td>
                                       
                  </tr>
                </aura:iteration> 
            </tbody>
        </table><br/>
        </article>
    </form>
       
      
</aura:component>
Helper:
({
    getCases : function(Component,event,helper) {
        var action= Component.get("c.getObject");
        var sessId = Component.get("v.recordId");
        
        action.setParams({
            "sessId" :sessId
        });
        action.setCallback(this,function(response){
            var state=response.getState();
                       
            if (Component.isValid() && state === "SUCCESS") {               
                //alert('Cases');
                 Component.set("v.cases",response.getReturnValue());
                 var caseCount = Component.get("v.cases").length;
                 Component.set("v.totalCases",caseCount);
                 console.log("caseCount "+caseCount);
                 var action1 = component.get("c.getmessagedt");
                 var cas = Componenet.get("v.cases");
                 action1.setParams({
                    "objCaseList":cas
                });
                action1.setCallback(this,function(response){
                var state=response.getState();
                       
                if (Component.isValid() && state === "SUCCESS") {               
                //alert('Cases');
                Component.set("v.LastEmail",response.getReturnValue());
                
            }
        });
           $A.enqueueAction(action1);     
            }
        });
        
        $A.enqueueAction(action);
    },
    navigateTorecordDetails :function(Component,event,helper){
       console.log('@@@ MovetoDetailPage method calls');
        var idstr = event.target.getAttribute("data-recId")
        console.log('@@@ CaseId ==> ' + idstr);
       var navEvt = $A.get("e.force:navigateToSObject");
       if(navEvt!=undefined){
           navEvt.setParams({
               "recordId": idstr
           });
           navEvt.fire();
       }
        $A.enqueueAction(action);
    }
})

getting error as "This page has an error. You might just need to refresh it. Error in $A.getCallback() [component is not defined] Callback failed: apex://CaseRelatedListApex/ACTION$getObject" 
Please help me to fix the error.

Thanks,
Sirisha
Raj VakatiRaj Vakati
Youtt componet name should match .. javascript in case sensitive 

Try this code
 
({
    getCases : function(Component,event,helper) {
        var action= Component.get("c.getObject");
        var sessId = Component.get("v.recordId");
        
        action.setParams({
            "sessId" :sessId
        });
        action.setCallback(this,function(response){
            var state=response.getState();
                       
            if (Component.isValid() && state === "SUCCESS") {               
                //alert('Cases');
                 Component.set("v.cases",response.getReturnValue());
                 var caseCount = Component.get("v.cases").length;
                 Component.set("v.totalCases",caseCount);
                 console.log("caseCount "+caseCount);
                 var action1 = Component.get("c.getmessagedt");
                 var cas = Component.get("v.cases");
                 action1.setParams({
                    "objCaseList":cas
                });
                action1.setCallback(this,function(response){
                var state=response.getState();
                       
                if (Component.isValid() && state === "SUCCESS") {               
                //alert('Cases');
                Component.set("v.LastEmail",response.getReturnValue());
                
            }
        });
           $A.enqueueAction(action1);     
            }
        });
        
        $A.enqueueAction(action);
    },
    navigateTorecordDetails :function(Component,event,helper){
       console.log('@@@ MovetoDetailPage method calls');
        var idstr = event.target.getAttribute("data-recId")
        console.log('@@@ CaseId ==> ' + idstr);
       var navEvt = $A.get("e.force:navigateToSObject");
       if(navEvt!=undefined){
           navEvt.setParams({
               "recordId": idstr
           });
           navEvt.fire();
       }
        $A.enqueueAction(action);
    }
})

 
SFDC New learnerSFDC New learner
Hi Raj Vakati,
I changed the code but still getting error as "Unable to find action 'getmessagedt' on the controller of c:CaseRelatedListComponent"
Naveen KNNaveen KN
Hi there, 

This error shows that you have not defined the method within apex code.

I see that you have referred the class 'CaseRelatedListApex' in your component and you are calling 'getmessagedt' method from the component [var action1 = component.get("c.getmessagedt")]. 

Check if this method ''getmessagedt'' is available in the apex code.