0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コールアウトするApexバッチを時間起動で動かす

Last updated at Posted at 2025-02-13

始めはスケジュールされたフローを使う予定でしたが、エラーを解消できなかったです。

    /*
     * 053の申請を取得する Flowから呼び出す
     */
    @InvocableMethod(label='get_Jobcan053B2')
    public static void get_Jobcan053B2() {
        
        
        //BAT_jobcan_calloutがなくても、実行できるように改造 2024-12-27    
        //更に改造 2025-01-15
		List<Jobcan053__c> insertList53 = jobcan_callout2.calljobcan_org2List();
        List<Jobcan053__c> upsertJList53 = jobcan_callout2.calljobcan_org2b(insertList53);//upsert insertList53 JobcanId__c;
        List<Jobcan053__c> upsertJList53B = jobcan_callout6.Jobcan053toTaskBatchB(upsertJList53);
        //if (upsertJList53B.size() >0 ) upsert upsertJList53B JobcanId__c;
        system.debug('upsertJList53B==>' + upsertJList53B);
        List<Jobcan053__c> upsertJList53C = jobcan_callout5.calljobcan_org2B(upsertJList53B);
        system.debug('upsertJList53C==>' + upsertJList53C);
        if (upsertJList53C.size() >0 ) upsert upsertJList53C JobcanId__c;
        
    }     

やっぱりスケジュールできるApexにします。

public void execute(SchedulableContext SC) {
       
       //BAT_jobcan_calloutがなくても、実行できるように改造 2024-12-27    
        //更に改造 2025-01-15
		List<Jobcan053__c> insertList53 = jobcan_callout2.calljobcan_org2List();
        List<Jobcan053__c> upsertJList53 = jobcan_callout2.calljobcan_org2b(insertList53);//upsert insertList53 JobcanId__c;
        List<Jobcan053__c> upsertJList53B = jobcan_callout6.Jobcan053toTaskBatchB(upsertJList53);
        //if (upsertJList53B.size() >0 ) upsert upsertJList53B JobcanId__c;
        system.debug('upsertJList53B==>' + upsertJList53B);
        List<Jobcan053__c> upsertJList53C = jobcan_callout5.calljobcan_org2B(upsertJList53B);
        system.debug('upsertJList53C==>' + upsertJList53C);
        if (upsertJList53C.size() >0 ) upsert upsertJList53C JobcanId__c;
      
   }

匿名ウィンドウで実行

ScheduledJobcan053 s = new ScheduledJobcan053();
String sch = '0 30 9 * * ?';
String jobID = System.schedule('Jobcan 053', sch, s);

起動できたかを確かめます

SELECT TimesTriggered, NextFireTime,CronExpression,State,CronJobDetail.Name,CronJobDetail.jobType FROM CronTrigger

ちゃんと起動してますね。

image.png

ああああ、エラーです。ええええ、サポートしてない?何で?

System.CalloutException: Callout from scheduled Apex not supported.

スケジュールされたジョブからコールアウトを行うことはできません。これを修正するには、スケジュールされたジョブから future または queueable を呼び出します。

global class SchedulableExchangeRate implements Schedulable {
    global void execute(SchedulableContext ctx){
        CurrencyExchangeController.calloutCurrency();
    }
}


public class CurrencyExchangeController {
    @future(callout=true) public static void calloutCurrency(){
    //here is the code of my method
    }
}

書き換えようと思ったけど、保存できません

このスケジュール可能なクラスに待機中または処理中のジョブがあります。CronTrigger ID (08eBC000008UZ8q)

以下でジョブを止めたら保存できました

System.abortJob('08eBC000008UZ8q');

何故かエラーになってスケジュールができません

Dependent class is invalid and needs recompilation: Class ScheduledJobcan053 : Static method cannot be referenced from a non static context: void CurrencyExchangeController.calloutCurrency()

以下に書き換えたらOKになった。

public void execute(SchedulableContext SC) {
       
       //CurrencyExchangeController newCurExcContr = new CurrencyExchangeController();
       //newCurExcContr.calloutCurrency();
       
       CurrencyExchangeController.calloutCurrency();
   }

無事に指定した時間で起動できました...

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?