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
sparsh bhatnagarsparsh bhatnagar 

Lightning error when single record present in custom list

hi ,
i am unable to perform any action when my custom list have only one record, i implimented my current solution to include dummy at initialization that worked with child cases related list but now i am dealing the same with Service Appointment and pervious solution is not working here. 

when ever there is only single record this errors occurs,  in case of multiple record there is no issue. you can see in console its picking up undefined .
below is my controller and apex code 
controller :- 
({
    doInit: function(component, event, helper) {
        // call the helper function on component load
       // helper.getChildRecords(component, event);
        var undefinedlisterrorhaldle =[];
        var recid = component.get("v.recordId");
        console.log(component.get("v.recordId"));
      
        undefinedlisterrorhaldle.push("08p9E0000009R1ZQAU");
        undefinedlisterrorhaldle.push("08p9E0000009R1ZQAA");
       console.log(undefinedlisterrorhaldle);
       component.set("v.AppointmentRecordList",undefinedlisterrorhaldle);
        console.log("check this ------", component.get("v.AppointmentRecordList"));
       // console.log(component.get("v.undefinedrelatedlisterror"));
       // console.log(component.get("v.AppointmentRecordList"));
        
    
        
      // this function call on the component load first time     
      // get the page Number if it's not define, take 1 as default
      var page = component.get("v.page") || 1;
      // get the select option (drop-down) values.   
      var recordToDisply = component.find("recordSize").get("v.value");
      // call the helper function   
      helper.getChildrecords(component, page, recordToDisply);
 
   },
  selectAll : function(component, event, helper)
    {
        var headercheck = event.getSource().get("v.value");
        var childCmp = component.find("check"); // return array
        console.log("Check Child " +childCmp);
        console.log("ChildCmp" +childCmp.length);
        if(headercheck){
            for(var i = 0;i < childCmp.length;i++){
              childCmp[i].set("v.value", true);
            }
        }
        else{
                for(var i =0;i < childCmp.length;i++){
                  childCmp[i].set("v.value", false);
                }
        }
    },

helper:-  
({getChildrecords : function(component, page, recordToDisply) {
 
      // create a server side action. 
      var action = component.get("c.getcase");
      // set the parameters to method 
       var selectedcaseids = component.get("v.recordId");
      action.setParams({"pageNumber": page,"recordToDisply": recordToDisply,"recordId": selectedcaseids
      });
      // set a call back   
      action.setCallback(this, function(a) {
         // store the response return value (wrapper class insatance)  
         var result = a.getReturnValue();
         console.log('result ---->' + JSON.stringify(result));
         // set the component attributes value with wrapper class properties.   
 
         component.set("v.AppointmentRecordList", result.ServiceAppointments);
         component.set("v.page", result.page);
         component.set("v.total", result.total);
         component.set("v.pages", Math.ceil(result.total / recordToDisply));
         component.set("v.AccountId", result.AccountId1);
      });
      // enqueue the action 
      $A.enqueueAction(action);
   },
apex : - public class Appointments {

    
     public class casePagerWrapper {
    @AuraEnabled public Integer pageSize {get;set;}
    @AuraEnabled public Integer page {get;set;}
    @AuraEnabled public Integer total {get;set;}
    @AuraEnabled public List<ServiceAppointment> ServiceAppointments {get;set;}
        @AuraEnabled public List<Project__c> AccountId1 {get;set;}
   } 
      
    
     @AuraEnabled
 public static casePagerWrapper getcase(Decimal pageNumber ,Integer recordToDisply, Id recordId) {
      Integer pageSize = recordToDisply;
      Integer offset = ((Integer)pageNumber - 1) * pageSize;
             
    // create a instance of wrapper class
    casePagerWrapper obj =  new casePagerWrapper();
    // set the pageSize,Page(Number), total records and case List(using OFFSET)   
        obj.pageSize = pageSize;
        obj.page = (Integer) pageNumber;
     obj.total = [select count() from ServiceAppointment where Project__c=: recordid ];
    //system.debug('The query is' + sSoql);
    obj.ServiceAppointments =  [SELECT Id, Status,AccountId,AppointmentNumber, OwnerId, ArrivalWindowStartTime,ArrivalWindowEndTime, Owner.Username FROM ServiceAppointment WHERE Project__c =: recordid   LIMIT :recordToDisply OFFSET :offset];   
     obj.AccountId1 = [select Account__c from Project__c where id =: recordid ];   
     //Database.query(sSoql);
     //return the wrapper class instance .
        return obj;
     }
    
   
}

component :- 
<aura:component controller="Appointments" implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:appHostable" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="AppointmentRecordList" type="Object[]"  />
    <aura:attribute name="undefinedrelatedlisterror" type="object[]" /> 
    <aura:attribute name="recordId" type="Id" default="a0B9E000000fcpTUAQ"/>
    <aura:attribute name="checkrecord" type="Integer"/>    
    <aura:attribute name="disableopencasebutton" type="boolean" />
   <aura:attribute name="page" type="integer" description="using for store page Number"/>
   <aura:attribute name="pages" type="integer" description="using for store All Pages page Number"/>
   <aura:attribute name="total" type="integer" description="total records count store "/>
    <aura:attribute name="AccountId" type="Object[]" />
    <aura:attribute name="selectedTabsoft" type="string" default="CaseNumber" description="Use for show/hide arraow sign on header based on conditions"/>
    
       
           
           <div class= "slds-box">
        <div class="slds-page-header slds-m-around_none slds-grid slds-wrap" >
              <div class="slds-media slds-size_3-of-6">
                  <div class="slds-media__figure">
                  <lightning:icon iconName="standard:service_appointment" size="small" /> 
                </div>
                    <div class="slds-media__body " >
                <h1 class="slds-page-header__title slds-truncate  slds-align-middle" title="Service Appointment"> Service Appointment ({!v.total})</h1>
                  </div>
            </div>
        
      <div class="slds-float--right slds-size_3-of-6">
        <div class="slds-button-group" role="group">
                <ui:button aura:id="button" class="slds-button slds_form_element__control"
                   label="New Appointment" press="{!c.NewChild}" />
                 </div>
                               <div class="slds-button-group">
                    <ui:button aura:id="button" class="slds-button slds_form_element__control"
                       label="Email Appointment Details" press="{!c.EmailChild}" />   
                    </div>  
            </div>
        </div>
               
      <p class="slds-truncate slds-size_3-of-6">
         <ui:inputSelect class="slds-select--small " aura:id="recordSize" label="Display Record Per Page: "  change="{!c.onSelectChange}">
            <ui:inputSelectOption text="10" label="10" value="true"/>
            <ui:inputSelectOption text="15" label="15"/>
            <ui:inputSelectOption text="20" label="20"/>
             </ui:inputSelect></p>
    <aura:if isTrue="{!v.total &gt; 0}">
    <table class="slds-table slds-table_bordered slds-table_fixed-layout slds-max-small-table--stacked-horizontal" role="grid">
      <thead>    
         <tr class="slds-text-title--caps">
            <th style="width:3.25rem;" class="slds-text-align--right">
               <div class="slds-form-element">
                  <div class="slds-form-element__control">
                     <label class="slds-checkbox">
                        <!--header checkbox for select all-->
                        <ui:inputCheckbox aura:id="box3" change="{!c.selectAll}"/>
                        <span class="slds-checkbox--faux"></span>
                        <span class="slds-form-element__label text"></span>
                     </label>
                  </div>
               </div>
            </th>
             <th class="slds-is-sortable slds-is-resizable slds-text-title--caps slds-size_1-of-10" scope="col" style="width: 4.5rem;" onclick="{!c.sortCaseNumber}">
               <span class="slds-truncate " title="Appointment Number">Appointment Number</span>  
                     </th>
             <th class="slds-is-sortable slds-is-resizable slds-text-title--caps slds-size_1-of-10" scope="col" style="width: 4.5rem;" onclick="{!c.sortCaseNumber}">
               <span class="slds-truncate " title="Status">Status </span>  
                     </th>
             <th class="slds-is-sortable slds-is-resizable slds-text-title--caps slds-size_1-of-10" scope="col" style="width: 4.5rem;" onclick="{!c.sortCaseNumber}">
               <span class="slds-truncate " title="ARRIVAL WINDOW START">ARRIVAL WINDOW START </span>  
                     </th>
             <th class="slds-is-sortable slds-is-resizable slds-text-title--caps slds-size_1-of-10" scope="col" style="width: 4.5rem;" onclick="{!c.sortCaseNumber}">
               <span class="slds-truncate " title="ARRIVAL WINDOW END">ARRIVAL WINDOW END </span>  
             </th></tr>
             </thead>
             <tbody>
                 
         <aura:iteration items="{!v.AppointmentRecordList}" var="con">
            <tr>
               <td scope="row" class="slds-text-align--right" style="width:3.25rem;">
                  <div class="slds-form-element">
                     <div class="slds-form-element__control">
                        <label class="slds-checkbox">
                           <ui:inputCheckbox text="{!con.Id}" aura:id="check" value="" />
                           <span class="slds-checkbox--faux"></span>
                           <span class="slds-form-element__label text"></span>
                        </label>
                     </div>
                  </div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!con.Appointmentnumber}"><a href ="{!'/one/one.app?#/sObject/'+ con.Id + '/view'}">{!con.AppointmentNumber}</a></div>
               </td>
               <td scope="row">
                  <div class="slds-truncate" title="{!con.Status}">{!con.Status}</div>
               </td>
                <td scope="row">
                  <div class="slds-truncate" title="{!con.ArrivalWindowStartTime}">{!con.ArrivalWindowStartTime}</div>
               </td>
                <td scope="row">
                  <div class="slds-truncate" title="{!con.ArrivalWindowEndTime}">{!con.ArrivalWindowEndTime}</div>
               </td>
                
            </tr>
         </aura:iteration>
             </tbody>
   </table>
sparsh bhatnagarsparsh bhatnagar
User-added image