⏰ Scheduling Tasks
The N/task module allows you to trigger Map/Reduce or Scheduled scripts from other scripts.
const task = require('N/task');
// Create a Map/Reduce task
const mrTask = task.create({
taskType: task.TaskType.MAP_REDUCE,
scriptId: 'customscript_my_mapreduce',
deploymentId: 'customdeploy_my_mapreduce'
});
// Submit the task
const taskId = mrTask.submit();
log.audit('Task Submitted', 'Task ID: ' + taskId);📊 Checking Task Status
// Check the status of a submitted task
const taskStatus = task.checkStatus({
taskId: taskId
});
log.audit('Task Status', taskStatus.status);
// Possible values: PENDING, PROCESSING, COMPLETE, FAILEDTask Status Values
| Status | Description |
|---|---|
| PENDING | Task is queued but not started |
| PROCESSING | Task is currently running |
| COMPLETE | Task finished successfully |
| FAILED | Task encountered an error |
🔧 Task Types
| TaskType | Use Case |
|---|---|
| MAP_REDUCE | Large-scale data processing |
| SCHEDULED_SCRIPT | Scheduled script execution |
| CSV_IMPORT | Import CSV data |
| ENTITY_DEDUPLICATION | Merge duplicate records |
| QUERY | Run saved queries |
✅ Common Pattern
Trigger a Map/Reduce from a Suitelet button click, then poll task.checkStatus() to show progress to the user.
📝 Passing Parameters
// Pass script parameters via deployment
const mrTask = task.create({
taskType: task.TaskType.MAP_REDUCE,
scriptId: 'customscript_process_orders',
deploymentId: 'customdeploy_process_orders',
params: {
custscript_customer_id: customerId,
custscript_start_date: startDate
}
});
mrTask.submit();📖 Finding This in the Docs
To look up task scheduling:
- Go to docs.oracle.com → SuiteScript 2.x Modules
- Click on N/task Module
- Review task.create(), task.checkStatus(), and TaskType enum
Key pages to bookmark:
- task.create(options) → taskType, scriptId, deploymentId, params
- Task.submit() → returns task ID for tracking
- task.checkStatus(options) → check PENDING, PROCESSING, COMPLETE, FAILED
- task.TaskType enum → MAP_REDUCE, SCHEDULED_SCRIPT, CSV_IMPORT
- TaskStatus object → status property values
🎯 Key Takeaways
- task.create() creates a task object with script and deployment IDs
- task.submit() returns a task ID for tracking
- task.checkStatus() returns PENDING, PROCESSING, COMPLETE, or FAILED
- Pass parameters via the params object
- Commonly used to trigger Map/Reduce from Suitelets
- Poll checkStatus() to show progress to users