I'm trying to setup a callout class to properly handle the passing in of a few variables to build an endpoint string. As you can see below I'm struggling with how and where to declare certain variables. With the below code, anywhere else the variables are referenced (methods) I get an error saying the variables don't exist. I'm not sure what the best practice is here with everything you see below. What should be managed in a constructor? What doesn't need to be managed in a constructor? Is a constructor necessary at all? What should be static? Final? Errors out at request.setEndpoint(accountTrans) Errors out at opp.AccountId = accountId;
Both state 'Variable does not exist'
I know this is an issue of initialization. As the endpoint string that I have the variable accountAddress in returns null.
public class EtherscanRESTCallouts {
public String accountAddress;
public Id accountId;
public String accountTrans;
private static final API_Token__mdt API_RECORD = [SELECT API_Token__c, MasterLabel FROM API_Token__mdt WHERE MasterLabel = 'Etherscan' WITH SECURITY_ENFORCED];
private static final String SINGLE_ACCOUNT_BALANCE = 'https://api.etherscan.io/api?module=account&action=balance&' +
'address=0x267f70f9b856de226fea5fc1b0a8e319c72ceff5&tag=latest&apikey=' + API_RECORD.API_Token__c;
private static final String MULTI_ACCOUNT_BALANCE = 'https://api.etherscan.io/api?module=account&action=balancemulti&' +
'address=0x267f70f9b856de226fea5fc1b0a8e319c72ceff5,0x1b3cb81e51011b549d78bf720b0d924ac763a7c2,0x73bceb1cd57c711feac4224d062b0f6ff338501e&tag=latest&apikey=' + API_RECORD.API_Token__c;
public EtherscanRESTCallouts(String accountName, Id accId) {
accountAddress = accountName;
accountId = accId;
accountTrans = 'https://api.etherscan.io/api?module=account&action=txlist&' +
'address=' + accountAddress + '&startblock=0&endblock=99999999&sort=asc&apikey=' + API_RECORD.API_Token__c;
}
private static Decimal toEth(String wei) {
Decimal ethAmount = Decimal.valueOf(wei).divide(10^18, 2, System.RoundingMode.UP);
return ethAmount;
}
@future(callout=true)
public static void accountTransactions() {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(accountTrans);
request.setMethod('GET');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
HttpResponse response = http.send(request);
// Parse the JSON response
if (response.getStatusCode() == 200) {
List<Opportunity> opportunitiesToInsert = new List<Opportunity>();
JSONParser parser = JSON.createParser(response.getBody());
EtherscanJSONHelper ethInstance = new EtherscanJSONHelper(parser);
for(EtherscanJSONHelper.Result result : ethInstance.result) {
Opportunity opp = new Opportunity();
opp.Block_Number__c = Integer.valueOf(result.blockHash);
opp.Time_Stamp__c = Datetime.newInstance(Long.valueOf(result.timeStamp));
opp.Hash__c = result.hash;
opp.Nonce__c = Integer.valueOf(result.nonce);
opp.Block_Hash__c = result.blockHash;
opp.Transaction_Index__c = Integer.valueOf(result.transactionIndex);
opp.From_Address__c = result.from_Z;
opp.To_Address__c = result.to;
opp.Transaction_Amount__c = toEth(result.value);
opp.Gas_Price_in_Eth__c = toEth(result.gasPrice);
opp.isError__c = Boolean.valueOf(result.isError);
opp.Transaction_Rec__c = Boolean.valueOf(result.txreceipt_status);
opp.Input__c = result.input;
opp.Contract_Address__c = result.contractAddress;
opp.Cumulative_Gas_Used__c = Integer.valueOf(result.cumulativeGasUsed);
opp.Gas_Used__c = Integer.valueOf(result.gasUsed);
opp.Confirmations__c = Integer.valueOf(result.confirmations);
opp.AccountId = accountId;
opportunitiesToInsert.add(opp);
}
if(!opportunitiesToInsert.isEmpty()) {
try {
insert opportunitiesToInsert;
} catch (DMLException e) {
ApexPages.addMessages(e);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'djrdev@icloud.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('djrdev@icloud.com');
mail.setSenderDisplayName('Apex error message');
mail.setSubject('Error from Org : ' + UserInfo.getOrganizationName());
mail.setPlainTextBody(e.getMessage());
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
} else {
System.debug(response.getBody());
}
}