1

I am trying to get both starting and ending calendar years of standard Period object in Salesforce. The idea is that fiscal year starts in February of the current year and ends in January next year (e.g. Feb2020-January2021). Both start and end dates can be easily determined by filtering Period object where Type = 'Year'.

I have a Fiscal Year picklist value on LWC page (FY2019, FY2020, FY2021), which represents the starting year. To match the returned data type of CALENDAR_YEAR(StartDate) from the query, I omitted first two characters from the selected picklist value, then converted the returned string into Integer.

Now challenge I am encountering is that result does not return anything. I made sure that there are rows returned in Salesforce Workbench.

Following is the result I am getting in my browser's developer console for the javascript:

enter image description here

Following are the current codes I have:

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 getFYOrgDataFromApex from '@salesforce/apex/PeriodController.getFYOrgData'; 


export default class periodLWC extends LightningElement {

  
    @track periodData = [];

    @track error;
   
    @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) {
           
            getFYOrgDataFromApex({orgStartFYInt})
            .then(result => {
                if(result) {
                    console.log('Returned results===>' + result);        
                }
                
            })
            .catch(error => {
                this.error = error;
            })    
        }
        
    }
    
}

PeriodController.cls

public class PeriodController{

       @AuraEnabled(cacheable=true)

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

              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) =: orgYear
                     GROUP BY Id, Type, CALENDAR_YEAR(EndDate), CALENDAR_YEAR(StartDate)  ];
              
              if(aggList.size() > 0) {

                     for(AggregateResult agg : aggList) {
                            
                            Date fyStartYear = (Date) agg.get('startYear');
                            Date fyEndYear = (Date) agg.get('endYear');

                            Integer intFyStartYear = fyStartYear.year();
                            Integer intFyEndYear = fyEndYear.year();

                            intList.add(intFyStartYear);
                            intList.add(intFyEndYear);              
                     }
                     
              }

              return intList;  
                           
       }
}

1

Problem 1: Parameter Name Mismatch

Your Apex has the signature:

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

But you call it with:

   getFYOrgDataFromApex({orgStartFYInt})

The names must match, and are case sensitive.

Problem 2: Invalid Runtime Conversion

The CALENDAR_YEAR function (and others like it, such as DAY_ONLY) return an Integer value, not a full date. Your Apex needs to change as follows:

  for (AggregateResult agg : aggList) {
    Integer intFyStartYear = (Integer) agg.get('startYear');
    Integer intFyEndYear = (Integer) agg.get('endYear');

    intList.add(intFyStartYear);
    intList.add(intFyEndYear);
  }

P.S. if (aggList.size() > 0) is not necessary. It is perfectly legal to iterate over an empty list.

1
  • I totally missed out those two important things. Results are now returned perfectly upon applying your suggestions. Thanks for your immediate response and help :) – Ertyq Jun 21 '20 at 6:48

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.