Apex @Futureアノテーションのサンプル

環境
Salesforce

説明
非同期で実行するメソッドを特定するには Future アノテーションを使用します。
Future を指定すると、Salesforce に使用可能なリソースが存在するときにこのメソッドが実行されます。

操作例
1.クラスのメソッドを非同期に実行するには、Future アノテーションのあるメソッドを定義します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
global class TestFutureClass {
@Future
static void tMethod(String a, Integer i) {
System.debug('Method called with: ' + a + ' and ' + i);
// 長時間実行されるコードを実行する
}
}
global class TestFutureClass { @Future static void tMethod(String a, Integer i) { System.debug('Method called with: ' + a + ' and ' + i); // 長時間実行されるコードを実行する } }
global class TestFutureClass {

  @Future 
  static void tMethod(String a, Integer i) {
    System.debug('Method called with: ' + a + ' and ' + i);
    // 長時間実行されるコードを実行する
  }
}

2.Future メソッドでコールアウトを許可するには、(callout=true) を指定します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@Future (callout=true)
public static void doCalloutFromFuture() {
//コールアウトを実行するコードを追加する
}
@Future (callout=true) public static void doCalloutFromFuture() { //コールアウトを実行するコードを追加する }
@Future (callout=true)
  public static void doCalloutFromFuture() {
   //コールアウトを実行するコードを追加する
}

 

Apex

Posted by arkgame