Week 7 • Module 14

N/task Module

Scheduling Map/Reduce and Scheduled scripts programmatically

⏰ 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, FAILED

Task Status Values

StatusDescription
PENDINGTask is queued but not started
PROCESSINGTask is currently running
COMPLETETask finished successfully
FAILEDTask encountered an error

🔧 Task Types

TaskTypeUse Case
MAP_REDUCELarge-scale data processing
SCHEDULED_SCRIPTScheduled script execution
CSV_IMPORTImport CSV data
ENTITY_DEDUPLICATIONMerge duplicate records
QUERYRun 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:

  1. Go to docs.oracle.com → SuiteScript 2.x Modules
  2. Click on N/task Module
  3. Review task.create(), task.checkStatus(), and TaskType enum

Key pages to bookmark:

🎯 Key Takeaways