I wants to use wrapper class object record list in lightning component and iterate over it to display the field values in all records as table. PFB the code that i have written. The problem response.getReturnValue() gives the list size as 0 but my list already contains 3 elements.


<aura:component implements="force:lightningQuickAction,force:hasRecordId,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" controller="OpportunityMachineViewController">  
    <aura:attribute name="sObjectInfo" type="Opportunity"/>
    <aura:attribute name="fieldsArray" type="List" default="['Name','OwnerId','CurrencyIsoCode','AccountId']"/>
    <aura:attribute name="recordId" type="String"/>  
    <aura:attribute name="PYCheck" type="Boolean" default="false"/>
    <aura:attribute name="PYListSize" type ="Integer"/>
    <aura:attribute name="PYListObj" type="OpportunityMachineViewController.prodYearWrapper[]" default="[]"/>

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <h3 class="slds-section-title--divider">Opportunity Details</h3>
    <div class="slds-form--stacked slds-p-around--medium slds-m-bottom--x-small">
        <div class="slds-form-element">
            <lightning:recordForm recordId="{!v.recordId}" objectApiName="Opportunity" mode="readonly" fields="{!v.fieldsArray}" columns="2"/>          
    <h3 class="slds-section-title--divider">Production Year</h3>
    <div class="slds-form--stacked slds-p-around--medium slds-m-bottom--x-small">
        <div class="slds-form-element">
            <aura:if isTrue="{!v.PYCheck}">   
                <aura:iteration items="{!v.PYListObj}" var="item">
                <lightning:button label="Delete" iconName="utility:Delete" variant="border-filled" onClick="{!c.DeleteAnYear}"/>
            <aura:set attribute="else">
                <lightning:button label="Add Year" iconName="utility:Add" variant="border-filled" onClick="{!c.AddAnYear}"/>


    doInit : function(component, event, helper) {
        var action= component.get("c.getProductionYear");
            "OppId" :"{!v.recordid}"
            var status= response.getState();
            if(component.isValid() && status == "SUCCESS")
                var listOfRecord=  response.getReturnValue();
                    alert('Inside if');


Apex Controller:

public class OpportunityMachineViewController{

   public static List<prodYearWrapper> prodYearWraList{get;set;}
   public static integer pycount;
   public static integer pyQuantity ;
   public static string OppIdValue;
    public static List<prodYearWrapper> getProductionYear(String OppId){
        prodYearWraList = new List<prodYearWrapper>();
        List<Production_Year__c> productionYearList = [Select Id, isFRP__c, Name, Quantity__c, Opportunity__c from Production_Year__c Where Opportunity__c =:OppId order by CreatedDate,Name];
        pycount = 0;
        for(Production_Year__c pYear:productionYearList){
            prodYearWrapper pyWra = new prodYearWrapper();
                pyQuantity = pyQuantity + (Integer)pYear.Quantity__c;
            pyWra.pyNo = pycount;
            pyWra.prodYear = pYear;
            pycount = pycount + 1;
        return prodYearWraList;

     public void getAddYear(){
        integer i;
            DateTime dt = System.now();
               Date d = dt.Date();
               i = integer.valueOf(dt.year());
            String[] strDate=prodYearWraList[pycount-1].prodYear.Name.split('/');
            i = Integer.valueOf(strDate[2]);
            i = i+1;
        prodYearWrapper proYearWra = new prodYearWrapper();
        Production_Year__c proYear = new Production_Year__c();        
        proYear.Name = '12/31/'+string.valueOf(i);
        proYear.Opportunity__c = OppIdValue;
        proYearWra.prodYear = proYear;
        proYearWra.pyNo = pycount;
        pycount = pycount + 1;
    }//end of addYear  

       public class prodYearWrapper{
        public Production_Year__c prodYear{get;set;}
        public integer pyNo{get;set;}

        public prodYearWrapper(){
            prodYear = new Production_Year__c();
  • Have you tried debugging your APEX code, does productionYearList and prodYearWraList have any elements (> 0 size) ? – Ajinkya Phadnis May 15 '19 at 6:55
  • Yes there was an issue in apex code itself found that in debug..!! Thanks – Priya May 15 '19 at 7:38

You do not use merge syntax in component.set. Change those lines to use just valueProvider.attributeName syntax. We can also eliminate the if-else with a little bit of logic.

// if(listOfRecord.length>0){ // Removed If here
    component.set("v.PYCheck",    listOfRecord.length>0);
    component.set("v.PYListSize", listOfRecord.length);
    component.set("v.PYListObj",  listOfRecord);
// } else { // removed else here
    // component.set("v.PYCheck", false); // Combined with above logic
// }
| improve this answer | |
  • but it still shows the count of records = 0 – Priya May 15 '19 at 6:49

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.