+ Start a Discussion
nagendra kumar 21nagendra kumar 21 

How can i change my code to pull the previous months currency data

Hi Everyone, 
I have this Apex code which takes the current date currency value when i ran batch, but now my client want to see when ever i ran btach it should take previous month currency value

So how where and how shouuld i change the code to accomplish below req and code. please someone help me modify my code. 

Every single day, financial exchange rates change according to the markets. So what we do, is instead of trying to grab the latest exchange rate every day, we wait until the previous month ends. Then we grab the average exchange rate for that previous month and inputted it into sfdc.
So in this example: We wait until February 2019 is done. Then come march 1st, 2019 we will be able to take the average exchange rate for the entire month of February 2019 and input it into sfdc.

This means that currently, for USD opportunities that close in February 2019, the exchnage rate for the month of January 2019  is used to do the conversion until we are able to go back and update our systems to include the month of february 2019 exchange rate.

This is my code :- 

public class ForexCurrencyHandler {
    public void processData () {
        ExchangeRate exchangeRate = (ExchangeRate) JSON.deserialize(callPublicExchangeRateApi(), ExchangeRate.class);
        // Need to populate date manually because we cannot use the "date" name as a variable since it's a reserved keyword
        exchangeRate.currencyDate = Date.today(); 
        String jsonDatedConversionRate = createDatedConversionRateJSON(exchangeRate);       
    public String callPublicExchangeRateApi(){
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        HTTPResponse res = http.send(req);
        System.debug('callout 1 '+res.getStatuscode());
        return res.getBody();
    public String createDatedConversionRateJSON (ExchangeRate exchangeRate) {
        DatedConversionRate conversionRate = new DatedConversionRate();
        conversionRate.ConversionRate = exchangeRate.rates.get('USD');
        conversionRate.IsoCode = 'USD';
        conversionRate.StartDate = Date.today();
        return JSON.serializePretty(conversionRate);
    public void insertDatedConversionRate (String jsonDatedConversionRate) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v44.0/sobjects/DatedConversionRate/');
        String body = jsonDatedConversionRate;
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
        req.setHeader('Content-Type', 'application/json');
        HttpResponse res = h.send(req);
         System.debug('callout 2 '+res.getBody());
         System.debug('callout 2 '+res.getStatuscode());
    public class ExchangeRate
        public map<String,Decimal> rates;
        public String base;
        public Date currencyDate;
Abdulla d 5Abdulla d 5
Hi Nagendra, you can change the exchangeRate.currencyDate = Date.month()-1;