Skip to main content
Notice removed Draw attention by sfdcfox
Bounty Ended with Daniel Ballinger's answer chosen by sfdcfox
added 87 characters in body
Source Link
user55489
user55489

enter image description here

enter image description here

edited tags
Link
Daniel Ballinger
  • 103.6k
  • 40
  • 284
  • 609
added 2982 characters in body
Source Link
user55489
user55489

Batch class

global class CreateDatedConversionRate implements Database.Batchable<sObject>,Database.Stateful,Database.AllowsCallouts { private String sessionId; public string querystring{get;set;} global CreateDatedConversionRate(){ this.sessionId = querystring; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = ''; List<AggregateResult> startDateList = [SELECT StartDate FROM DatedConversionRate GROUP BY STARTDATE ORDER BY STARTDATE DESC LIMIT 1]; System.debug(LoggingLevel.INFO,'Last exchangeDate:'+startDateList.get(0)); String startDate = DateTime.newInstance(((Date)startDateList.get(0).get('StartDate')), Time.newInstance(0,0,0,0)).format('yyyy-MM-dd'); System.debug(LoggingLevel.INFO,'query string:'+startDate); query = 'SELECT IsoCode, ConversionRate FROM DatedConversionRate WHERE STARTDATE = '+ startDate +' ORDER BY IsoCode'; System.debug(LoggingLevel.INFO,'Executed query:'+query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<DatedConversionRate> conversionList) { System.debug(LoggingLevel.INFO,'conversionList size:'+conversionList.size()); String todayDate = DateTime.newInstance(System.Date.today(), Time.newInstance(0, 0, 0, 0)).format('yyyy-MM-dd'); if(conversionList != null && conversionList.size()>0){ for(DatedConversionRate conversionRate : conversionList){ String str = '' ; str +='{ "IsoCode" : "'; str += conversionRate.IsoCode +'", "ConversionRate" : '; str += conversionRate.ConversionRate + ', "StartDate" : "'; str += todayDate + '"'; str += '}'; /*REST API CALL TO INSERT RECORDS.*/ Http h = new Http(); HttpRequest req = new HttpRequest(); req.setBody(str); req.setEndpoint('callout:Createddateconversionrate/services/data/v43.0/sobjects/DatedConversionRate'); req.setMethod('POST'); System.debug(LoggingLevel.INFO,'request body:'+req.getBody()); if(!Test.isRunningTest()){ HttpResponse res = h.send(req); System.debug(LoggingLevel.INFO,'res'+res.getBody()); System.debug(LoggingLevel.INFO,'res'+res.getStatus()); } } } } global void finish(Database.BatchableContext BC) { } } 

Batch class

global class CreateDatedConversionRate implements Database.Batchable<sObject>,Database.Stateful,Database.AllowsCallouts { private String sessionId; public string querystring{get;set;} global CreateDatedConversionRate(){ this.sessionId = querystring; } global Database.QueryLocator start(Database.BatchableContext BC) { String query = ''; List<AggregateResult> startDateList = [SELECT StartDate FROM DatedConversionRate GROUP BY STARTDATE ORDER BY STARTDATE DESC LIMIT 1]; System.debug(LoggingLevel.INFO,'Last exchangeDate:'+startDateList.get(0)); String startDate = DateTime.newInstance(((Date)startDateList.get(0).get('StartDate')), Time.newInstance(0,0,0,0)).format('yyyy-MM-dd'); System.debug(LoggingLevel.INFO,'query string:'+startDate); query = 'SELECT IsoCode, ConversionRate FROM DatedConversionRate WHERE STARTDATE = '+ startDate +' ORDER BY IsoCode'; System.debug(LoggingLevel.INFO,'Executed query:'+query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<DatedConversionRate> conversionList) { System.debug(LoggingLevel.INFO,'conversionList size:'+conversionList.size()); String todayDate = DateTime.newInstance(System.Date.today(), Time.newInstance(0, 0, 0, 0)).format('yyyy-MM-dd'); if(conversionList != null && conversionList.size()>0){ for(DatedConversionRate conversionRate : conversionList){ String str = '' ; str +='{ "IsoCode" : "'; str += conversionRate.IsoCode +'", "ConversionRate" : '; str += conversionRate.ConversionRate + ', "StartDate" : "'; str += todayDate + '"'; str += '}'; /*REST API CALL TO INSERT RECORDS.*/ Http h = new Http(); HttpRequest req = new HttpRequest(); req.setBody(str); req.setEndpoint('callout:Createddateconversionrate/services/data/v43.0/sobjects/DatedConversionRate'); req.setMethod('POST'); System.debug(LoggingLevel.INFO,'request body:'+req.getBody()); if(!Test.isRunningTest()){ HttpResponse res = h.send(req); System.debug(LoggingLevel.INFO,'res'+res.getBody()); System.debug(LoggingLevel.INFO,'res'+res.getStatus()); } } } } global void finish(Database.BatchableContext BC) { } } 
Tweeted twitter.com/StackSalesforce/status/1017709196865830912
Notice added Draw attention by sfdcfox
Bounty Started worth 500 reputation by sfdcfox
added 110 characters in body
Source Link
user55489
user55489
Loading
Formatted
Source Link
Jayant Das
  • 30.8k
  • 3
  • 45
  • 69
Loading
Source Link
user55489
user55489
Loading