|
1 | | -# Queueable |
| 1 | +# Queueable API |
| 2 | + |
| 3 | +Apex classes `QueueableBuilder.cls`, `QueueableManager.cls`, and `QueueableJob.cls`. |
| 4 | + |
| 5 | +Common Queueable example: |
| 6 | + |
| 7 | +```apex |
| 8 | +QueueableJob job = new MyQueueableJob(); |
| 9 | +Async.AsyncResult result = Async.queueable(job) |
| 10 | + .priority(5) |
| 11 | + .delay(2) |
| 12 | + .continueOnJobExecuteFail() |
| 13 | + .enqueue(); |
| 14 | +System.debug('Job enqueued: ' + result.customJobId); |
| 15 | +``` |
| 16 | + |
| 17 | +## Methods |
| 18 | + |
| 19 | +The following are methods for using Async with Queueable jobs: |
| 20 | + |
| 21 | +[**INIT**](#init) |
| 22 | + |
| 23 | +- [`queueable(QueueableJob job)`](#queueable) |
| 24 | + |
| 25 | +[**Build**](#build) |
| 26 | + |
| 27 | +- [`asyncOptions(AsyncOptions asyncOptions)`](#asyncoptions) |
| 28 | +- [`delay(Integer delay)`](#delay) |
| 29 | +- [`priority(Integer priority)`](#priority) |
| 30 | +- [`continueOnJobEnqueueFail()`](#continueonjobequeuefail) |
| 31 | +- [`continueOnJobExecuteFail()`](#continueonjobexecutefail) |
| 32 | +- [`rollbackOnJobExecuteFail()`](#rollbackonjobexecutefail) |
| 33 | +- [`asSchedulable()`](#asschedulable) |
| 34 | + |
| 35 | +[**Execute**](#execute) |
| 36 | + |
| 37 | +- [`enqueue()`](#enqueue) |
| 38 | +- [`attachFinalizer()`](#attachfinalizer) |
| 39 | + |
| 40 | +[**Context**](#context) |
| 41 | + |
| 42 | +- [`getQueueableJobContext()`](#getqueueablejobcontext) |
| 43 | +- [`getQueueableChainBatchId()`](#getqueueablechainbatchid) |
| 44 | + |
| 45 | +### INIT |
| 46 | + |
| 47 | +#### queueable |
| 48 | + |
| 49 | +Constructs a new QueueableBuilder instance with the specified queueable job. |
| 50 | + |
| 51 | +**Signature** |
| 52 | + |
| 53 | +```apex |
| 54 | +Async queueable(QueueableJob job); |
| 55 | +``` |
| 56 | + |
| 57 | +**Example** |
| 58 | + |
| 59 | +```apex |
| 60 | +Async.queueable(new MyQueueableJob()); |
| 61 | +``` |
| 62 | + |
| 63 | +### Build |
| 64 | + |
| 65 | +#### asyncOptions |
| 66 | + |
| 67 | +Sets AsyncOptions for the queueable job. Cannot be used with delay(). |
| 68 | + |
| 69 | +**Signature** |
| 70 | + |
| 71 | +```apex |
| 72 | +QueueableBuilder asyncOptions(AsyncOptions asyncOptions); |
| 73 | +``` |
| 74 | + |
| 75 | +**Example** |
| 76 | + |
| 77 | +```apex |
| 78 | +AsyncOptions options = new AsyncOptions(); |
| 79 | +Async.queueable(new MyQueueableJob()) |
| 80 | + .asyncOptions(options); |
| 81 | +``` |
| 82 | + |
| 83 | +#### delay |
| 84 | + |
| 85 | +Sets a delay in minutes before the job executes. Cannot be used with asyncOptions(). |
| 86 | + |
| 87 | +**Signature** |
| 88 | + |
| 89 | +```apex |
| 90 | +QueueableBuilder delay(Integer delay); |
| 91 | +``` |
| 92 | + |
| 93 | +**Example** |
| 94 | + |
| 95 | +```apex |
| 96 | +Async.queueable(new MyQueueableJob()) |
| 97 | + .delay(5); // Execute in 5 minutes |
| 98 | +``` |
| 99 | + |
| 100 | +#### priority |
| 101 | + |
| 102 | +Sets the priority for the queueable job. Lower numbers = higher priority. |
| 103 | + |
| 104 | +**Signature** |
| 105 | + |
| 106 | +```apex |
| 107 | +QueueableBuilder priority(Integer priority); |
| 108 | +``` |
| 109 | + |
| 110 | +**Example** |
| 111 | + |
| 112 | +```apex |
| 113 | +Async.queueable(new MyQueueableJob()) |
| 114 | + .priority(1); // High priority |
| 115 | +``` |
| 116 | + |
| 117 | +#### continueOnJobEnqueueFail |
| 118 | + |
| 119 | +Allows the job chain to continue even if this job fails to enqueue. |
| 120 | + |
| 121 | +**Signature** |
| 122 | + |
| 123 | +```apex |
| 124 | +QueueableBuilder continueOnJobEnqueueFail(); |
| 125 | +``` |
| 126 | + |
| 127 | +**Example** |
| 128 | + |
| 129 | +```apex |
| 130 | +Async.queueable(new MyQueueableJob()) |
| 131 | + .continueOnJobEnqueueFail(); |
| 132 | +``` |
| 133 | + |
| 134 | +#### continueOnJobExecuteFail |
| 135 | + |
| 136 | +Allows the job chain to continue even if this job fails during execution. |
| 137 | + |
| 138 | +**Signature** |
| 139 | + |
| 140 | +```apex |
| 141 | +QueueableBuilder continueOnJobExecuteFail(); |
| 142 | +``` |
| 143 | + |
| 144 | +**Example** |
| 145 | + |
| 146 | +```apex |
| 147 | +Async.queueable(new MyQueueableJob()) |
| 148 | + .continueOnJobExecuteFail(); |
| 149 | +``` |
| 150 | + |
| 151 | +#### rollbackOnJobExecuteFail |
| 152 | + |
| 153 | +Rolls back any DML operations if this job fails during execution. |
| 154 | + |
| 155 | +**Signature** |
| 156 | + |
| 157 | +```apex |
| 158 | +QueueableBuilder rollbackOnJobExecuteFail(); |
| 159 | +``` |
| 160 | + |
| 161 | +**Example** |
| 162 | + |
| 163 | +```apex |
| 164 | +Async.queueable(new MyQueueableJob()) |
| 165 | + .rollbackOnJobExecuteFail(); |
| 166 | +``` |
| 167 | + |
| 168 | +#### asSchedulable |
| 169 | + |
| 170 | +Converts the queueable builder to a schedulable builder for cron-based scheduling. For scheduling, look into the [SchedulableBuilder](/api/schedulable.md) API. |
| 171 | + |
| 172 | +**Signature** |
| 173 | + |
| 174 | +```apex |
| 175 | +SchedulableBuilder asSchedulable(); |
| 176 | +``` |
| 177 | + |
| 178 | +**Example** |
| 179 | + |
| 180 | +```apex |
| 181 | +Async.queueable(new MyQueueableJob()) |
| 182 | + .asSchedulable(); |
| 183 | +``` |
| 184 | + |
| 185 | +### Execute |
| 186 | + |
| 187 | +#### enqueue |
| 188 | + |
| 189 | +Enqueues the queueable job with the configured options. Returns an Async.AsyncResult. |
| 190 | + |
| 191 | +**Signature** |
| 192 | + |
| 193 | +```apex |
| 194 | +Async.AsyncResult enqueue(); |
| 195 | +``` |
| 196 | + |
| 197 | +**Example** |
| 198 | + |
| 199 | +```apex |
| 200 | +Async.AsyncResult result = Async.queueable(new MyQueueableJob()) |
| 201 | + .priority(5) |
| 202 | + .enqueue(); |
| 203 | +``` |
| 204 | + |
| 205 | +#### attachFinalizer |
| 206 | + |
| 207 | +Attaches a finalizer job to run after the current job completes. Can only be called within a QueueableChain context. |
| 208 | + |
| 209 | +**Signature** |
| 210 | + |
| 211 | +```apex |
| 212 | +Async.AsyncResult attachFinalizer(); |
| 213 | +``` |
| 214 | + |
| 215 | +**Example** |
| 216 | + |
| 217 | +```apex |
| 218 | +// Inside a QueueableJob's work() method |
| 219 | +Async.queueable(new MyFinalizerJob()) |
| 220 | + .attachFinalizer(); |
| 221 | +``` |
| 222 | + |
| 223 | +### Context |
| 224 | + |
| 225 | +#### getQueueableJobContext |
| 226 | + |
| 227 | +Gets the current queueable job context, providing access to job information and Salesforce QueueableContext. |
| 228 | + |
| 229 | +**Signature** |
| 230 | + |
| 231 | +```apex |
| 232 | +Async.QueueableJobContext getQueueableJobContext(); |
| 233 | +``` |
| 234 | + |
| 235 | +**Example** |
| 236 | + |
| 237 | +```apex |
| 238 | +Async.QueueableJobContext ctx = Async.getQueueableJobContext(); |
| 239 | +QueueableJob currentJob = ctx.currentJob; |
| 240 | +QueueableContext sfContext = ctx.queueableCtx; |
| 241 | +``` |
| 242 | + |
| 243 | +#### getQueueableChainBatchId |
| 244 | + |
| 245 | +Gets the ID of the QueueableChain batch job if the current execution is part of a batch-based chain. |
| 246 | + |
| 247 | +**Signature** |
| 248 | + |
| 249 | +```apex |
| 250 | +Id getQueueableChainBatchId(); |
| 251 | +``` |
| 252 | + |
| 253 | +**Example** |
| 254 | + |
| 255 | +```apex |
| 256 | +Id batchId = Async.getQueueableChainBatchId(); |
| 257 | +System.debug('Current batch ID: ' + batchId); |
| 258 | +``` |
0 commit comments