0

In one method I set the value of a static variable, in the other I want to get it, but I get null:

public class CurrencyExchangeController {
    
    public static String requestDate;

    @future(callout=true)
    public static void calloutCurrency() {
        ...
        System.debug(requestDate); //this is null
    }

    @AuraEnabled(cacheable=true)
    public static List<Exchange_Rate__c> getLastExchangeRates (){
        ...
        requestDate = 'test';
    }

1 Answer 1

3

That's expected. Since future methods are in a separate transaction, whatever value was stored before won't exist, because static variables aren't preserved across transactions. You need to simply pass in the value as a parameter:

@future(callout=true)
public static void calloutCurrency(String requestDate) {
    ...
    System.debug(requestDate);
}

@AuraEnabled(cacheable=true)
public static List<Exchange_Rate__c> getLastExchangeRates (){
    ...
    calloutCurrency('test');
}
3
  • Thanks. And there are no workarounds? My method initially took a parameter, as you noted in your answer. However, I have two similar methods and I wanted to optimize this by combining it into one, without using the parameter that the method should take. Commented Jun 6, 2021 at 19:44
  • 1
    @uniqueuser The future method needs the parameter, full stop. A typical solution is to write two functions that call the same function, with differing paremeters: static void callWithoutParam() { calloutCurrency(null); } callWithParam(String param) { calloutCurrency(param); }
    – sfdcfox
    Commented Jun 6, 2021 at 20:00
  • Thanks for the clarification, I will do so. Commented Jun 7, 2021 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .