Modules

filter

 

Objects/Functions

module (N/task)

SuiteScript 2.0

Main Examples

N/task Module
Member Type: Name
Search NetSuite - https://system.netsuite.com/app/help/helpcenter.nl?search=N/task Module

// Code Example 1
/**
* @NApiVersion 2.x
*/

require(['N/task', 'N/runtime', 'N/email'], function(task, runtime, email) {
function submitMapReduceDeployment() {

// Store the script ID of the script to submit
//
// Update the following statement so it uses the script ID
// of the map/reduce script record you want to submit
var mapReduceScriptId = 'customscript_test_mapreduce_script';
log.audit('mapreduce id: ', mapReduceScriptId);

// Create a map/reduce task
//
// Update the deploymentId parameter to use the script ID of
// the deployment record for your map/reduce script
var mrTask = task.create({
taskType: task.TaskType.MAP_REDUCE,
scriptId: mapReduceScriptId,
deploymentId: 'customdeploy_test_mapreduce_script'
});

// Submit the map/reduce task
var mrTaskId = mrTask.submit();

// Check the status of the task, and send an email if the
// task has a status of FAILED.
//
// Update the authorId value with the internal ID of the user
// who is the email sender. Update the recipientEmail value
// with the email address of the recipient.
var taskStatus = task.checkStatus(mrTaskId);
if (taskStatus.status === 'FAILED') {
var authorId = -5;
var recipientEmail = 'notify@myCompany.com';
email.send({
author: authorId,
recipients: recipientEmail,
subject: 'Failure executing map/reduce job!',
body: 'Map reduce task: ' + mapReduceScriptId + ' has failed.'
});
}
}

submitMapReduceDeployment();
});

// Code Example 2
/**
* @NApiVersion 2.x
*/

require(['N/task'], function(task) {
// Do one of the following:
//
// - Create a saved search and capture its ID. To do this, you can use
// the following code snippet (replacing the id, filters, and columns
// values as appropriate):
//
// var mySearch = search.create({
// type: search.Type.SALES_ORDER,
// id: 'customsearch_my_search',
// filters: [...],
// columns: [...]
// });
// mySearch.save();
// var savedSearchId = mySearch.searchId;
//
// - Use the ID of an existing saved search. This is the approach that
// this script sample uses. Update the following statement with the
// internal ID of the search you want to use.
var savedSearchId = -10;

// Create the search task
var myTask = task.create({
taskType: task.TaskType.SEARCH
});
myTask.savedSearchId = savedSearchId;

// Specify the ID of the file that search results will be exported into
//
// Update the following statement so it uses the internal ID of the file
// you want to use
myTask.fileId = 448;

// Submit the search task
var myTaskId = myTask.submit();

// Retrieve the status of the search task
var taskStatus = task.checkStatus({
taskId: myTaskId
});

// Optionally, create new variables to represent values used previously in
// this script. You may want to use these variables in additional logic you
// add to this script.
var myFileId = taskStatus.fileId;
var mySavedSearchId = taskStatus.savedSearchId;

// Optionally, add logic that executes when the task is complete
if (taskStatus.status === task.TaskStatus.COMPLETE) {
// Add any code that is appropriate. For example, if this script created
// a saved search, you may want to delete it.
}
});

// Code Example 3
/**
* @NApiVersion 2.x
*/

require(['N/task'], function(task) {
// Specify a file for the search results
var asyncSearchResultFile = 'SuiteScripts/ExportFile.csv';

// Create a scheduled script task
var scheduledScript = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT
});
scheduledScript.scriptId = 'customscript_as_ftr_ss';
scheduledScript.deploymentId = 'customdeploy_ss_dpl';
scheduledScript.params = {
'custscript_ss_as_srch_res' : asyncSearchResultFile
};

// Create a map/reduce script task
var mapReduceScript = task.create({
taskType: task.TaskType.MAP_REDUCE
});
mapReduceScript.scriptId = 'customscript_as_ftr_mr';
mapReduceScript.deploymentId = 'customdeploy_mr_dpl';
mapReduceScript.params = {
'custscript_mr_as_srch_res' : asyncSearchResultFile
};

// Create the search task
var asyncTask = task.create({
taskType: task.TaskType.SEARCH
};
asyncTask.savedSearchId = 'customsearch35';
asyncTask.filePath = asyncSearchResultFile;

// Add dependent scripts to the search task before it is submitted
asyncTask.addInboundDependency(scheduledScript);
asyncTask.addInboundDependency(mapReduceScript);

// Submit the search task
var asyncTaskId = asyncTask.submit();
});

// Code Example 4
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/file', 'N/log', 'N/email', 'N/runtime'], function(file, log, email, runtime) {
// Load the search results file and send an email with the file attached and
// the number of rows in the file

function execute(context) {
// Read a CSV file and return the number of rows minus the header row
function numberOfRows(csvFileId) {
var invoiceFile = file.load({
id: csvFileId
});
var iterator = invoiceFile.lines.iterator();
var noOfLines = 0;

// Skip the first row (the header row)
iterator.each(function() {
return false;
});

// Process the rest of the rows
iterator.each(function() {
noOfLines++;
return true;
});

return noOfLines;
}

// Send an email to the user who ran the script, and attach the
// CSV file with the search results
function sendEmailWithAttachment(csvFileId) {
var noOfRows = numberOfRows(csvFileId);
var userId = runtime.getCurrentUser().id;
var fileObj = file.load({
id: csvFileId
});

email.send({
author: userId,
recipients: userId,
subject: 'Search completed',
body: 'CSV file attached, ' + noOfRows + ' record(s) found.',
attachments: [fileObj]
});
}

// Retrieve the ID of the search results file
//
// Update the name parameter to use the script ID of the original
// search task
var resFileId = runtime.getCurrentScript().getParameter({
name: 'custscript_ss_as_srch_res'
});

if (!resFileId) {
log.error('Could not obtain file content from the specified ID.');
return;
}

log.debug({
title: 'search - numberOfRows',
details: numberOfRows(resFileId)
});
sendEmailWithAttachment(resFileId);
}

return {
execute: execute
};
});

// Code Example 5
/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
*/
define(['N/runtime', 'N/file', 'N/log', 'N/email'], function(runtime, file, log, email) {
// Load the search results file, count the number of letters in the file, and
// store this count in another file

function getInputData() {
// Retrieve the ID of the search results file
//
// Update the completionScriptParameterName value to use the script
// ID of the original search task
var completionScriptParameterName = 'custscript_mr_as_srch_res';
var resFileId = runtime.getCurrentScript().getParameter({
name: completionScriptParameterName
});

if (!resFileId) {
log.error({
details: 'resFileId is not valid. Please check the script parameter stored in the completionScriptParameterName variable in getInputData().'
});
}

return {
type: 'file',
id: resFileId
};
}

function map(context) {
var email = context.value.split(',')[1];
if ("Email" !== email) {
var splitEmail = email.split('@');
context.write(splitEmail[splitEmail.length-1], 1);
}
}

function reduce(context) {
context.write(context.key, context.values.length);
}

function summarize(summary) {
var type = summary.toString();
log.audit({title: type + ' Usage Consumed ', details: summary.usage});
log.audit({title: type + ' Concurrency Number ', details: summary.concurrency});
log.audit({title: type + ' Number of Yields ', details: summary.yields});

var contents = '';
summary.output.iterator().each(function(key, value) {
contents += (key + ' ' + value + '\n');
return true;
});

// Create the output file
//
// Update the name parameter to use the file name of the output file
var fileObj = file.create({
name: 'domainCount.txt',
fileType: file.Type.PLAINTEXT,
contents: contents
});

// Specify the folder location of the output file, and save the file
//
// Update the fileObj.folder property with the ID of the folder in
// the file cabinet that contains the output file
fileObj.folder = -15;
fileObj.save();
}

return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});

// Code Example 6
/**
* @NApiVersion 2.x
*/

require(['N/task'], function(task) {
var recordActionTask = task.create({
taskType: task.TaskType.RECORD_ACTION
});
recordActionTask.recordType = 'timebill';
recordActionTask.action = 'approve';
recordActionTask.params = [
{recordId: 1, note: 'This is a note for 1'},
{recordId: 5, note: 'This is a note for 5'},
{recordId: 23, note: 'This is a note for 23'}
];

var handle = recordActionTask.submit();

var res = task.checkStatus({
taskId: handle
}); // Returns a RecordActionTaskStatus object
log.debug('Initial status: ' + res.status);
});

//SOURCE: https://system.netsuite.com/app/help/helpcenter.nl?fid=section_4345787858.html