[英]It is necessary to perform the planned action through the apex scheduler
我有這個代碼:
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 點創建這些對象。
告訴我如何通過頂點調度程序實現它。
您需要一個implements Schedulable
Schedulable 的 class。 可以是這個 class,只需將其添加到 header 並添加execute
方法即可。 或者可以分開class,沒關系。
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();
}
}
一旦您的 class 保存正常,您應該能夠從 UI(設置 -> Apex 類 -> 按鈕)或使用開發者控制台的System.schedule
1-liner 安排它,例如。
另一種方法是有一個預定的 Flow——但是讓你的 Apex 可以從 Flow 調用需要更多的工作。
PS 不要將變量命名為test
。 最終一段時間后,您可能需要像Test.isRunningTest()
這樣的“真實”東西,這將是“有趣的”。 這就像寫作
Integer Account = 5; // happy debugging suckers
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.