Skip to content

Instantly share code, notes, and snippets.

@goravseth
Created October 2, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save goravseth/8c4727fb180d36981f4aa3335d46f6c6 to your computer and use it in GitHub Desktop.
Save goravseth/8c4727fb180d36981f4aa3335d46f6c6 to your computer and use it in GitHub Desktop.
Pulls current rates and updates CurrencyType records in SFDC
public class CurrencyUpdater {
list <CurrencyType> ctl = new list<CurrencyType>([SELECT Id,IsoCode,ConversionRate FROM CurrencyType WHERE IsActive = TRUE AND IsCorporate = FALSE]);
map <string, CurrencyType> ctMap = new map<String, CurrencyType>();
public CurrencyUpdater () {
for (CurrencyType ct : ctl){
ctMap.put(ct.IsoCode,ct);
}
}
public class CurrentRate {
string IsoCode;
decimal value;
}
public class myJSONClass{
public string disclaimer;
public string license;
public string timestamp;
public string base;
// public currentRate[] rates = new list<currentRate>();
public map<string,decimal> rates = new map<string,decimal>();
}
public list<CurrencyType> callEndpoint() {
list <currencyType> ctUpdateList = new list <currencyType>();
HttpRequest req = new HttpRequest();
//Set HTTPRequest Method
req.setMethod('GET');
//Set HTTPRequest header properties
req.setEndpoint('callout:OpenExchangeRates');
//Set the HTTPRequest body
//req.setBody(body);
Http http = new Http();
try {
//Execute web service call here
HTTPResponse res = http.send(req);
//Helpful debug messages
//System.debug('res to string' + res.toString());
System.debug('STATUS:'+res.getStatus());
System.debug('STATUS_CODE:'+res.getStatusCode());
string responseText = res.getBody();
//system.debug('responseText = ' + responseText);
myJSONClass obj = (myJSONClass)JSON.deserialize(responseText, myJSONClass.class);
// system.debug(obj.rates.keySet());
FOR (currencyType ct : ctl){
//system.debug('old rate = ' + ct.IsoCode +' ' +ct.ConversionRate);
IF (obj.rates.get(ct.IsoCode) != null ){
ct.ConversionRate = obj.rates.get(ct.IsoCode);
ct.IsActive = TRUE;
//system.debug('new rate = ' + ct.IsoCode + ' ' + ct.ConversionRate);
ctUpdateList.add(ct);
string ctJSON = JSON.serialize(ct);
callSFDCAPI(ctJSON, ct.Id);
}
// system.debug('ctid = ' + ct.id);
}
// update(ctUpdateList); ILLEGAL!! try this instead -->
// https://salesforce.stackexchange.com/questions/13294/patch-request-using-apex-httprequest/13300#13300
} catch(System.CalloutException e) {
//Exception handling goes here....
}
return ctUpdateList;
}
/* no dice
public class runFlow{
//Instance of the Flow
public Flow.Interview.UpdateCurrencyTypes myFlow {get; set;}
public void start(list<CurrencyType> input) {
list<CurrencyType> ctList = new list<CurrencyType>();
ctList.addAll(input);
Map<String, Object> params = new Map<String, Object>();
params.put('socCurrencyTypes', ctList);
// https://andyinthecloud.com/2014/10/26/calling-flow-from-apex/
myFlow = new Flow.Interview.UpdateCurrencyTypes(params);
myFlow.start();
}
}
*/
public void callSFDCAPI(string input, id recId) {
string inputJSON = input;
id crId = recId;
system.debug('inputJSON = ' + inputJSON);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v41.0/sobjects/CurrencyType/' + crId + '?_HttpMethod=PATCH' );
req.setMethod('POST');
req.setBody(inputJSON);
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
req.setHeader('X-HTTP-Method-Override', 'PATCH');
HttpResponse res = h.send(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment