0

I have a task in front of me: for the time period specified by the user, you need to get currency rates from a third-party service and display the results on the screen. I wrote the following code:

//this method gets the dates, makes a request to the API service, and adds new records to the database

@future(callout=true)
    public static void calloutExchangeRatesForDate(Set<String> missingDates){
        String currencies = convertToString(getPicklistValues());
        List<Exchange_Rate__c> listData = new List<Exchange_Rate__c>();
        if(!missingDates.isEmpty()){
            for(String requestDate : missingDates){
                String requestEndpoint = getApiCurrencyExchange() + requestDate + '&latest?base=GBP&symbols=' + currencies;
                System.debug('полный API с датой: ' + requestEndpoint);
                listData.add(makeRequest(requestEndpoint));
            }
        }
        newRecordExchangeRates(listData);
    }

//this method gets two dates (the beginning of the period and the end of the period), calculates the records for which the dates are missing in the database, and sends the missing ones to the method I wrote above. After that, it makes a request to the database again to extract the records, taking into account the recently added ones. However, it does not receive new entries.

@AuraEnabled
    public static List<Exchange_Rate__c> getExchangeRatesForPeriod(Date dateFrom, Date dateTo){
        List<Exchange_Rate__c> listData = getFromDBExchangeRatesForPeriod(dateFrom, dateTo);
        Set<Date> existingDates = new Set<Date>();
        Set<String> missingDates = new Set<String>();
        for (Exchange_Rate__c rate: listData) {
            existingDates.add(rate.Date__c);
        }
        Date nextDate = dateFrom;
        while (nextDate <= dateTo) {
            if (!existingDates.contains(nextDate)) {
                missingDates.add(String.valueOf(nextDate).removeEnd(' 00:00:00'));
            }
            nextDate = nextDate.addDays(1);
        }
        calloutExchangeRatesForDate(missingDates);
        return getFromDBExchangeRatesForPeriod(dateFrom, dateTo);
    }

As far as I understand , this is due to the fact that the future method and I can't get the data right away. Or can it be done?

4
  • Why are you using @future for what sounds like should be a synchronous call?
    – identigral
    Jun 10 at 15:33
  • I'm not sure if this is correct, I'm a beginner... If I remove @future, I get an error: "You have uncommitted work pending. Please commit or rollback before calling out" Jun 10 at 15:44
  • 1
    The "You have uncommitted work" error is due to a rule that a synchronous callout can't happen after a DML statement. If you change the order of these steps and do a synchronous callout first then your DML, you won't see the uncommitted error. Whether this sequence of steps will work in your architecture is another question. Another option is to have a batch job retrieve the rates independently from the UI.
    – identigral
    Jun 10 at 16:32
  • Thanks. I'm sorry, but I didn't understand... Could you change my example as you wrote it? Jun 18 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.