1

I have two dates (the start date of the period and the end date) that the user enters in the form to get a list of data. I need to get a list of this data from the database and check if there is a record every day. I need to fix the dates for which there are no records, so that I can do further manipulations with them later.

//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){
            let action = component.get('c.getListExchangeRatesForThePeriod');
                action.setParams({ "dateFrom" : dateFrom,
                                  "dateTo" : dateTo });
                action.setCallback(this, function(response) {
                    let state = response.getState();
                    if (state === "SUCCESS") {
                    ...
                    }
                });
        $A.enqueueAction(action);
        }
    }

//apxc

//here I need to determine the missing dates

@AuraEnabled
    public static List<Exchange_Rate__c> getListExchangeRatesForThePeriod(Date dateFrom, Date dateTo){
        String listFields = convertToString(getFieldsNameForRequest());
        List<Exchange_Rate__c> listData = Database.query('SELECT Date__c, Base_Currency__c, ' + listFields + ' FROM Exchange_Rate__c WHERE Date__c >= :dateFrom and Date__c <= :dateTo');
        return listData;
    }
1

You need to know the existing dates, you need to check possible dates against the existing dates. Easy enough to brute force. You'll also need more than one return value, to give both the list of objects and the list of missing dates, so a slightly different structure would be used.


public class SomeClass {
    @AuraEnabled
    public List<Exchange_Rate__c> rates;
    @AuraEnabled
    public List<Date> missingDates;
    
    public SomeClass getExchangeRates(Date dateFrom, Date dateTo) {
        String listFields = convertToString(getFieldsNameForRequest());
        //Here's all of your list data.
        List<Exchange_Rate__c> listData = Database.query('SELECT Date__c, Base_Currency__c, ' + listFields + ' FROM Exchange_Rate__c WHERE Date__c >= :dateFrom and Date__c <= :dateTo');
        SomeClass returnVal = new SomeClass();
        //List data added to return value.
        returnVal.rates = listData;
        Set<Date> existingDates = new Set();
        Set<Date> missingDates = new Set();
        for (Exchange_Rate__c rate: listData) {
            existingDates.add(rate.Date__c);
        }
        Date nextDate = dateFrom;
        while (nextDate <= dateTo) {
            if (!existingDates.contains(nextDate)) {
                missingDates.add(nextDate);
            }
            nextDate = nextDate.addDays(1);
        }
        //missing dates added to return value
        returnVal.missingDates = missingDates;
        return returnVal;
    }
}
1
  • Thank you for your help, this is what I need! Jun 5 at 14:08

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.