To trigger a Map/Reduce script from a User Event script in NetSuite, you can use the N/task module in SuiteScript 2.0 or 2.1.
Sample code:
define([‘N/record’, ‘N/search’, ‘N/log’, ‘N/task’],
(record, search, log, task) => {
const afterSubmit = (scriptContext) => {
try {
if (scriptContext.type !== scriptContext.UserEventType.CREATE && scriptContext.type !== scriptContext.UserEventType.EDIT) {
return;
}
let billPaymentRecord = scriptContext.newRecord;
let billPaymentId = billPaymentRecord.id;
log.debug(“billPaymentId”, billPaymentId)
let mrTaskId = createMapReduceTask(billPaymentId);
} catch (e) {
log.error(‘Error Retrieving Applied Records’, e);
}
}
const createMapReduceTask = (billPaymentId) => {
try {
let mrTask = task.create({ taskType: task.TaskType.MAP_REDUCE });
mrTask.scriptId = “customscript_jj_mr_attach_file_payment”;
mrTask.deploymentId = “customdeploy_jj_mr_attach_file_payment”;
mrTask.params = {
“custscript_jj_bill_payment_id”: billPaymentId
};
return mrTask.submit();
} catch (e) {
log.error(‘Error in createMapReduceTask’, e);
return null;
}
};
return { afterSubmit }
});
The passed parameters from userevnt script can be accessed in Map Reduce script.
Map Reduce script:
define([‘N/file’, ‘N/record’, ‘N/runtime’, ‘N/search’, ‘N/log’],
(file, record, runtime, search, log) => {
const getInputData = (inputContext) => {
try {
let billPaymentRecordId = runtime.getCurrentScript().getParameter({ name: ‘custscript_jj_bill_payment_id’ });
} catch (e) {
log.error(‘Error in getInputData’, e.message);
return [];
}
}