[Salesforce]バッチスケジュール

Salesforceのバッチスケジュールについて共有します。

1.バッチサンプル

global with sharing class Batch_sample implements Database.Batchable<sObject>
{
  private String query;
  
  //コンストラクタ
  global Batch_sample(String q){
  	system.debug('query ---:'+query);
      query = q;
  }
  
  //データ取得
  global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator(query);
  }
  
  //開始
  global void execute(Database.BatchableContext BC, List<sObject> scope){
  
      system.debug('scope.size() ---'+scope.size());//バッチの処理を記述
      executeLogic(scope);
      
  }
  
  //完了
  global void finish(Database.BatchableContext BC)
  {
  	
      //完了後の処理があれば
      String jobName = 'Batch_sample';
      
		CronTrigger ct = getCronTrigger1(jobName);

		if (ct != null) {
			System.abortJob(ct.Id);
		}

      Datetime dt = Datetime.now().addMinutes(2);
      ID pid = System.schedule(jobName, dt.format('0 mm HH dd MM ? yyyy'), new Schedule_sample());
  
  }
  
  public virtual void executeLogic(List<sObject> listSObj){
  	
  	List<Account> accs = new List<Account>();
  	List<customObject__c> customs = new List<customObject__c>();

  	for(sObject sObj : listSobj){
  		Account acc = new Account();
  		acc.Id = String.valueOf(sObj.get('sfid__c'));
  		acc.Dummy__c = Boolean.valueOf(sObj.get('Dummy__c'));
  		accs.add(acc);

  		customObject__c custom = new customObject__c();
  		custom.Id = String.valueOf(sObj.get('Id'));
  		custom.IsProcessed__c = true;
  		customs.add(custom);
  	}
  	
  	try {
  		if(accs.size() > 0){
  			system.debug('executeLogic --- accs.size() --- : '+accs.size());
  			update accs;
  		}
  		if(customs.size() > 0){
  			system.debug('executeLogic --- customs.size() --- : '+customs.size());
  			update customs;
  		}
  	} catch (Exception e) {
  		System.debug(e.getMessage());
  	}
  	
  }

	public static CronTrigger getCronTrigger1(String jobName) {

		List<CronTrigger> result = [
			select		Id
			from		CronTrigger
			where		CronJobDetail.JobType = '7'					// JobType(7) = Scheduled Apex
			and			CronJobDetail.Name =: jobName
			limit		1
		];

		return (result.isEmpty())? null: result[0];

	}


}

2.バッチスケジュールサンプル

global class Schedule_sample implements Schedulable {
  global void execute(SchedulableContext ctx) {
      String soql = 'SELECT Id, SfId__c, Dummy__c, IsProcessed__c FROM customObject__c Where IsProcessed__c = false Order By Id Limit 300';
      Batch_sample b = new Batch_sample(soql);
      Database.executeBatch(b, 200);
  }
}

3.バッチテストクラス

@isTest (SeeAllData = false)
private class TestBatch_sample {

	// 同期バッチテスト1 (スケジュールクラステスト)
	static testMethod void myUnitTest1() {

		List<Account> accs = new List<Account>();
		for(Integer i =1; i<=100; i++ ){
			Account acc = new Account();
			acc.Name = 'test' + String.ValueOf(i);
			accs.add(acc);
		}
		insert accs;

		List<Account> accList = new List<Account>();
		accList = [SELECT Id From Account];		
		List<customObject__c> customs = new List<customObject__c>();
		for(Integer i =0; i<100; i++ ){
			customObject__c custom = new customObject__c();
			custom.SfId__c = String.ValueOf(accList.get(i).Id);
			custom.Dummy__c = true;
			custom.IsProcessed__c = false;
			customs.add(custom);
		}
		insert customs;
		
		test.startTest();
		ID pid = System.schedule('TestBatch_sample', Datetime.now().addMinutes(2).format('0 mm HH dd MM ? yyyy'), new Schedule_sample());
		test.stopTest();
		

	}


    
}

投稿者: kinkun

保有資格 Salesforce Certified Platform App Builder T Salesforce Certified Platform Developer I Salesforce Certified Platform Developer II Salesforce Certified Administrator

コメントを残す

メールアドレスが公開されることはありません。