LoginSignup
0
0

More than 1 year has passed since last update.

Apexバッチのサンプル

Last updated at Posted at 2022-04-27

質問の答えにできるようなシンプルなコードを準備しておきたいと思います。

Write a Batch Apex to Update all the Accounts with Industry as "Banking" to "Energy".

public class BAT_Update_Account implements Database.Batchable<sObject> {

    public Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute

        String query = 'Select Id,Name From Account where type =\'Banking\'';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, List<Account> accList) {

        System.debug('******** accList ' + accList);
        List<Account> updateAccountList = new List<Account>();

        for (Account a : accList ){
            Account acc = new Account();
            acc.Id = a.Id;
            acc.Type ='Energy';
            updateAccountList.add(acc);
        }

        

        try {       
               

            if(updateAccountList.size() > 0) update updateAccountList;

        } catch(Exception e) {
            System.debug(e);
        }

    }  

    public void finish(Database.BatchableContext BC) {
    }

}
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