Job
Methods of the Job object and of job requests, which run a server script in the background on a job queue.
Job is the global that submits a server script to run in the background on a job queue. Use it from any server script when work takes too long to run while a user waits, or must run as another user. Scheduled jobs, which run on a cron expression, are records described in Scheduled jobs and job queues in the Administrator guide.
Get an instance
Job.request() returns a job request. Set its options, then call submit(), which creates a Job request record and returns its ID.
let requestId = Job.request()
.name("Recalculate open tickets")
.script("log.info('Running in the background');")
.submit();
A request goes to the Default queue unless you name another. The “Job request poller” job picks up waiting requests every ten seconds.
Job functions
| Method | Returns | Description | Since |
|---|---|---|---|
Job.request() |
Job request | A new job request to configure and submit. | 1.11 |
Job.pollRequests() |
Nothing | Starts a poll for waiting requests now, without waiting for it to finish. | 1.11 |
Job.getDefaultQueueName() |
String | The name of the default queue, "Default". |
1.11 |
Job.getJobQueuePoolSizeLimit() |
Number | The largest pool size a job queue can have, from the system.job.queue.pool.size.limit application property. |
1.11 |
Job request methods
Each setter returns the request, so calls chain.
| Method | Returns | Description | Since |
|---|---|---|---|
script(script) |
Job request | The server script to run. | 1.11 |
name(name) |
Job request | A name that shows what the job is for in the job request list. | 1.11 |
queue(queueName) |
Job request | The job queue to run on, by the name of its Job queue record. | 1.11 |
withContext(context) |
Job request | An object whose properties become variables in the script. Pass values this way instead of building them into the script text. | 1.11 |
forRecord(record) |
Job request | A record to link to the request. The script receives it as current. |
1.11 |
onComplete(script) |
Job request | A server script to run when the request completes, in its own transaction. | 1.11 |
asCurrentUser() |
Job request | Runs the script as the user who submits the request. | 1.11 |
runAs(user) |
Job request | Runs the script as another user, given as a user record or the user’s ID. | 1.11 |
externalQueue(external) |
Job request | Sets the request’s ExternalQueue field. |
1.11 |
submit() |
String | Creates the job request record and returns its ID. | 1.11 |
Examples
Pass values to the script
withContext() keeps values out of the script text, so a value cannot change the code that runs.
Job.request()
.name("Greeting")
.script("log.info(greeting);")
.withContext({ greeting: "Hello" })
.submit();
// Writes "Hello" to the log when the job runs
Run work for a record as the current user
Servicely Script
Job.request()
.name("Recalculate costs")
.forRecord(current)
.asCurrentUser()
.script("current.update();")
.onComplete("log.info('Costs recalculated');")
.submit();
Response
"<record id>"