有必要通过顶点调度程序执行计划的操作



我有这样的代码:

public static String currencyConverter() {

allCurrency test = new allCurrency();
HttpRequest request = new HttpRequest();
HttpResponse response = new HttpResponse();
Http http = new Http();
request.setEndpoint(endPoint);
request.setMethod('GET');
response = http.send(request);

String JSONresponse = response.getBody();
currencyJSON currencyJSON = (currencyJSON)JSON.deserialize(JSONresponse, currencyJSON.class); 
test.USD = currencyJSON.rates.USD;
test.CAD = currencyJSON.rates.CAD;
test.EUR = currencyJSON.rates.EUR;
test.GBP = currencyJSON.rates.GBP;
Log__c logObject = new Log__c();
logObject.Status_Code__c = String.valueOf(response.getStatusCode());
logObject.Response_Body__c = response.getBody();

insert logObject;
if (response.getStatusCode() == 200) {
Exchange_Rate__c ExchangeRateObject = new Exchange_Rate__c();
ExchangeRateObject.CAD__c = currencyJSON.rates.CAD;
ExchangeRateObject.EUR__c = currencyJSON.rates.EUR;
ExchangeRateObject.GBP__c = currencyJSON.rates.GBP;
ExchangeRateObject.USD__c = currencyJSON.rates.USD;
ExchangeRateObject.Log__c = logObject.id;
insert ExchangeRateObject;
}
return JSON.serialize(test);
}

这里我得到不同的货币,然后在LWC中调用它们,我还创建了两个有值的对象。

我希望每天中午12点创建这些对象。

告诉我如何通过apex调度程序实现这一点。

您需要一个类implements Schedulable。可以是这个类,只需将其添加到header并添加execute方法。或者可以单独的类,没有太大关系。

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

之类的
public with sharing MyClass implements Schedulable{
public static String currencyConverter() { ... your method here }
public void execute(SchedulableContext sc) {
currencyConverter(); 
}
}

一旦你的类保存OK,你应该能够从UI (Setup ->Apex类->按钮)或System.schedule-liner从开发控制台。

另一种选择是有一个预定的Flow -但这需要更多的工作,使您的Apex可以从Flow中调用。

注:不要将变量命名为test。最终一段时间后,你可能需要"real";像Test.isRunningTest()这样的东西,那将是"有趣的"。就像写

Integer Account = 5; // happy debugging suckers