2

I have a list of integers and trying to assign each on javascript variables. But I am getting undefined results:

enter image description here

I am stuck on the following codes:

periodLWC.html

<template> 
    
    <lightning-combobox
        class="slds-m-bottom_small slds-m-left_small"
        name="fiscalYear"
        label="Fiscal Year"
        value={yearValue}
        placeholder="--None--"
        options={yearOptions}
        onchange={findOrgFYData}>
    </lightning-combobox>  
       
</template>

periodLWC.js

import { LightningElement, track} from 'lwc';


import getFYOrgData '@salesforce/apex/PeriodController.getFYOrgData'; 


export default class periodLWC extends LightningElement {

  
    @track periodData = [];

    @track error;

    @track orgFYData = [];

    @track orgFYstart1;
                    
    @track orgFYend1;
   
    @track yearValue = '--None--';

    @track yearOptions = [
        {value: 'FY2019', label: 'FY2019'},
        {value: 'FY2020', label: 'FY2020'},
        {value: 'FY2021', label: 'FY2021'}
    ];

    
    findOrgFYData(event) {
        const orgYear = event.target.value;

        var orgStartFY = orgYear.substring(2);

        console.log('Trimmed fiscal year value is===>' + orgStartFY);

        var orgStartFYInt = parseInt(orgStartFY, 10);

        console.log('Converted integer is===>' + orgStartFYInt);

        if(orgStartFYInt) {
           
            getFYOrgData({orgStartFYInt})
            .then(result => {
                if(result) {
                    console.log('Returned results===>' + result);      

                    this.orgFYData = result;
                    console.log('Passed result' + this.orgFYData);
                    this.orgFYstart1 = result.fyStartYear;
                    console.log('Start Year' + this.orgFYstart1);
                    this.orgFYend1 = result.fyEndYear;
                    console.log('End Year' + this.orgFYend1);  
                }
                
            })
            .catch(error => {
                this.error = error;
            })    
        }
        
    }
    
}

PeriodController.cls

public class PeriodController{

       @AuraEnabled(cacheable=true)

       public static List<Integer> getFYOrgData(Integer orgStartFYInt) {

              List<Integer> intList = new List<Integer>();
              List<AggregateResult> aggList = new List<AggregateResult>();

              aggList = [SELECT CALENDAR_YEAR(EndDate) endYear,Id,CALENDAR_YEAR(StartDate) startYear,Type FROM Period 
                     WHERE Type = 'Year' AND CALENDAR_YEAR(StartDate) =: orgStartFYInt
                     GROUP BY Id, Type, CALENDAR_YEAR(EndDate), CALENDAR_YEAR(StartDate)  ];
                

              for(AggregateResult agg : aggList) {
                            
                  Integer fyStartYear = (Integer) agg.get('startYear');
                  Integer fyEndYear = (Integer) agg.get('endYear');
       
                  intList.add(fyStartYear);
                  intList.add(fyEndYear);              
              }
                     
              return intList;  
                           
       }
}
4

You are returning the values in the result, not properties. so you need to use the result index to get value. in fact result.fyStartYear and result.fyEndYear do not exist instead use : result[0] and result[1]

    findOrgFYData(event) {
        const orgYear = event.target.value;

        var orgStartFY = orgYear.substring(2);

        console.log('Trimmed fiscal year value is===>' + orgStartFY);

        var orgStartFYInt = parseInt(orgStartFY, 10);

        console.log('Converted integer is===>' + orgStartFYInt);

        if(orgStartFYInt) {
           
            getFYOrgData({orgStartFYInt})
            .then(result => {
                if(result) {
                    console.log('Returned results===>' + result);      

                    this.orgFYData = result;
                    console.log('Passed result' + this.orgFYData);                    
                    this.orgFYstart1 = result[0];                    
                    console.log('Start Year' + this.orgFYstart1);                   
                    this.orgFYend1 = result[1];
                    console.log('End Year' + this.orgFYend1);  
                }
                
            })
            .catch(error => {
                this.error = error;
            })    
        }
        
    }

2
  • this line this.orgFYend1 = result.result[1]; does not return anything, so I modified it to this this.orgFYend1 = result[1];. But you have just given me the idea on how to do it. Both values are now returned, thanks! :)) – Ertyq Jun 21 '20 at 13:38
  • My bad it was just result[1], I updated the answer. – Klecool Jun 21 '20 at 13: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.