0

I need to pass the date period "from" "to" from the js controller to the apex method in order to extract a list of values from the database.

//cmp

...
    <aura:attribute name="dateFrom" type="Date"/>
    <aura:attribute name="dateTo" type="Date"/>
...

//js

getDataForThePeriod : function(component, helper){
        let dateFrom = component.get("v.dateFrom");
        let dateTo = component.get("v.dateTo");
        if(dateFrom != '' && dateTo != '' && dateFrom < dateTo){
            //here I need to contact apex to make a request for data for the period
        }
    },

//apxc

//Date__c - this is the field of the object for which I want to get data for a certain period.

//Also, should the method accept a single variable in the form of a period (String?) or should there be two of them (Date type)?

@AuraEnabled(cacheable=true)
    public static List<Exchange_Rate__c> getListExchangeRates (...){
        String currencys = converter(getListFieldsCurrenciesForRequest());
        return Database.query('SELECT Date__c, Base_Currency__c, ' + currencys + ' FROM Exchange_Rate__c WHERE Date__c...');
    }
0
1

you can directly pass date as a parameter in apex. And query like below code example.

@AuraEnabled(cacheable=true)
    public static List<Exchange_Rate__c> getListExchangeRates (Date toDate , Date fromDate){
        String currencys = converter(getListFieldsCurrenciesForRequest());
        return Database.query('SELECT Date__c, Base_Currency__c, ' + currencys + ' FROM Exchange_Rate__c WHERE Date__c >= :fromDate and Date__c <= :toDate ');
    }
3
  • I fixed the apex method, but now I get: System.QueryException: expecting a colon, found 'dateTo'. My js: let action = component.get('c.getListExchangeRatesForThePeriod'); action.setParams({"dateFrom" : dateFrom, "dateTo" : dateTo}); Jun 1 at 22:13
  • problem is in your query make sure to add colon WHERE Date__c >= :fromDate and Date__c <= :toDate Jun 2 at 4:41
  • 1
    Ah, yes, I missed this point in the "DateTo" condition of the apex method and thought the problem was related to the js method. Thank you for your help! Jun 2 at 11:13

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.