Apex Queueable Apex を使用したプロセスの制御サンプル
環境
Salesforce Apex
構文
Queueable Apex は、Queueable インターフェースを実装するだけで使用できます。
public class SomeClass implements Queueable {
public void execute(QueueableContext context) {
// 処理コード
}
}
使用例
取引先レコードのコレクションを取り、各レコードに parentId を設定してから、データベースのレコードを更新します。
public class UpdateParentAccount implements Queueable {
private List<Account> accounts;
private ID parent;
public UpdateParentAccount(List<Account> records, ID id) {
this.accounts = records;
this.parent = id;
}
public void execute(QueueableContext context) {
for (Account account : accounts) {
account.parentId = parent;
// 他の処理またはコールアウトを実行
}
update accounts;
}
}
使用例2
このクラスをジョブとしてキューに追加するには、次のコードを実行します。
// find all accounts in ‘NY’ List<Account> accounts = [select id from account where billingstate = ‘NY’]; // すべてのレコードの特定の親アカウントを検索する Id parentId = [select id from account where name = 'ACME Corp'][0].Id; // Queueable クラスの新しいインスタンスをインスタンス化します UpdateParentAccount updateJob = new UpdateParentAccount(accounts, parentId); // 処理のためにジョブをキューに入れる ID jobID = System.enqueueJob(updateJob);