When an account is saved I need to update it with value from a REST API. I though my approach would work but I'm getting the following compilation errors.
Method does not exist or incorrect signature: void getTicker() from the type tickerClass
What's the proper way to make a REST API callout in a trigger to update the trigger records?
I tried getting it to work like this, but got stuck on the compilation error.
- Person clicks New Account, and fills in the appropriate fields. Person clicks Save.
- triggerTicker on Account is triggered.
- triggerTicker calls tickerClass.
- tickerClass uses REST API to grab the 'high' value.
- tickerClass adds the 'high' value in the valueHigh variable and then assigns valueHigh to the Ticker_Start__c field.
- Record is saved.
My trigger.
trigger triggerTicker on Account (before insert) {
tickerClass.getTicker();
// convert the output of ticker.getTicker() from String to Decimal
Decimal newTicker = Decimal.valueOf(tickerClass.getTicker());
// create new Account record and include required fields
for (Account newAcc : Trigger.new) {
Account a = new Account();
a.Name = 'Ticker Test';
a.Ticker_Start__c = newTicker;
}
}
It calls a class (tickerClass) method getTicker(). I am getting the following Problem in the on triggerTicker LINE 'Decimal newTicker = Decimal.valueOf(ticker.getTicker());:
Here is tickerClass.getTicker():
public class tickerClass {
@future(callout=true)
public static void getTicker() {
String retVal = null;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.bitstamp.net/api/ticker/');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
system.debug(results);
retVal = string.valueof(results.get('high'));
Decimal valueHigh = decimal.valueOf(retVal);
}
}
}
UPDATE: UPDATED THE TRIGGER AND THE CLASS BUT STILL GETTING ERRORS. I AM HAVING ISSUES WITH JUST PASSING THE ACCOUNT FROM THE TRIGGER TO THE CLASS. IDEAS? CAN'T FIND ANYTHING IN TRAILHEAD.
Trigger (updated)
trigger triggerTicker on Account (after insert) {
tickerClass.getTicker(Trigger.new);
}
Class (updated)
public class tickerClass {
// have to call future method if you are going to pull the method in a trigger
@future(callout=true)
// because we are looking for the accountId we need to make the trigger an after trigger
public static void getTicker(Account[] accounts) {
// do callout stuff
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.bitstamp.net/api/ticker/');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
String retVal = null;
retVal = string.valueof(results.get('high'));
Decimal valueHigh = decimal.valueOf(retVal);
for (Account acct : accounts) {
acct.Ticker_Start__c = valueHigh;
}
}
// save the changes
update accounts;
}
}
tickerClass Error 'public static void getTicker(Account[] accounts) {' Future methods do not support parameter type of List Ok, I'm thinking I need to pass over the Account Id, right? How do I do that?
triggerTicker Error 'tickerClass.getTicker(Trigger.new);' Method does not exist or incorrect signature: void getTicker(List) from the type tickerClass I get one of these errors no matter what I do it seems. I have no idea how to properly pass data to the Class it seems...