0

I rewrote the code, through anonymous works. I only have a problem with the date and field Log__c (lookup).

public class ExampleExchange {

@AuraEnabled
public static List<Exchange_Rate__c> getExchangeRate() {
    List<Exchange_Rate__c> conData = [SELECT ID, Name, Base_Currency__c, Date__c, CAD__c, EUR__c, GBP__c, USD__c, Log__c FROM Exchange_Rate__c];

    return conData;
}


@future(callout=true)
public static void saveRate() {

    Http http = new Http();

    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://api.exchangeratesapi.io/latest');
    request.setMethod('GET');
    HttpResponse response = http.send(request);

    Log__c logResponse = new Log__c(

        Response_Body__c = response.getBody(),
        Status_Code__c = String.valueOf(response.getStatusCode())

    );
    insert logResponse;

    Map<String, Object> rootMap = (Map<String, Object>) JSON.deserializeUntyped(logResponse.Response_Body__c);
    String base = String.valueOf(rootMap.get('base'));
    Date date = Date.valueOf(rootMap.get('date'));
    Map<String, Object> ratesMap = (Map<String, Object>) rootMap.get('rates');

    Exchange_Rate__c rate = new Exchange_Rate__c();

    rate.Base_Currency__c = base;
    rate.Date__c = date;
    rate.CAD__c = (Double) ratesMap.get('CAD');
    rate.EUR__c = (Double) ratesMap.get('EUR');
    rate.GBP__c = (Double) ratesMap.get('GBP');
    rate.USD__c = (Double) ratesMap.get('USD');


    insert rate;


}

}

In "Date date = Date.valueOf(rootMap.get('date'));"

Error: Static method cannot be referenced from a non static context: Date Date.valueOf(Object)

How to write Log I do not understand.

  • 2
    Could you please edit to be specific about the issue you are having? Are you receiving an error, and if so what is it and on which line? – David Reed Mar 1 at 18:27
  • How are you able to save this class? You can not use a reserved date keyword. You should receive a compile error Identifier name is reserved: date. After renaming the attribute, i was able to save this class. How to reproduce the error? Because this code works for me, – Samir Mar 4 at 19:02
  • I cheated, but he returned my old mistake "Invalid date: 2020-03-04" – Алёна Опарина Mar 5 at 11:53
  • Answer: String dates = String.valueOf(rootMap.get('date')); Date d = date.valueOf(dates); – Алёна Опарина Mar 5 at 12:50
1

It seems that data type of Status_Code__c in Log__c is TEXT

Try below piece of code

Log__c logResponse = new Log__c(

            Response_Body__c = response.getBody(),
            Status_Code__c = String.valueOf(response.getStatusCode())

        );
 insert logResponse;

If this does not help, please post data type of Response_Body__c and Status_Code__c

| improve this answer | |
  • Thanks, it helped. But I'm not sure if it will enter data into the object. – Алёна Опарина Mar 2 at 6:23
  • It should enter the data. If it does not, then post your error here. – Samir Mar 2 at 6:24
  • There are no errors, but no data is added. – Алёна Опарина Mar 2 at 9:00
  • What is the response received?? – Samir Mar 2 at 9:13
  • Nothing. {"rates":{"CAD":1.7297075544,"EUR":1.1721268241,"USD":1.2866436148,"GBP":1.0},"base":"GBP","date":"2020-02-28"} This is a request, but it has not been recorded. – Алёна Опарина Mar 2 at 9:18

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.