Apex Queueable Apex を使用したプロセスの制御サンプル

環境
Salesforce Apex

構文
Queueable Apex は、Queueable インターフェースを実装するだけで使用できます。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class SomeClass implements Queueable {
public void execute(QueueableContext context) {
// 処理コード
}
}
public class SomeClass implements Queueable { public void execute(QueueableContext context) { // 処理コード } }
public class SomeClass implements Queueable {
    public void execute(QueueableContext context) {
        // 処理コード
    }
}

使用例
取引先レコードのコレクションを取り、各レコードに parentId を設定してから、データベースのレコードを更新します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
}
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; } }
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
このクラスをジョブとしてキューに追加するには、次のコードを実行します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// 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);
// 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);
// 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);

 

Apex

Posted by arkgame