/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
*/
//------------------------------------------------------------------
//Script: STC_ProcessQueue_MR.js
//------------------------------------------------------------------
define([ 'N/runtime', 'N/record', 'N/search'],
(runtime, record, search) => {
pad = (value) => {
if(value < 10) {
return '0' + value;
} else {
return value;
}
}
getInputData = (context) => {
log.debug('==START==', '==START==');
var now = new Date();
var modifier = now.getUTCHours() > 12 ? 'pm' : 'am';
var hourIn12 = now.getUTCHours() % 12 || 12;
var dateString = (now.getMonth()+1) + '/' + now.getDate() + '/' + now.getFullYear() + ' ' + hourIn12 + ':' + pad(now.getMinutes()) + ' ' + modifier;
var queueElementSearchObj = search.create({
type: "customrecord_stc_queue_element",
filters:
[
["custrecord_stc_queue_element_next_retry", search.Operator.ONORBEFORE, dateString], // only get records where the next retry date is in the past or now
"AND",
["custrecord_stc_queue_element_status","anyof","1","2"], // queued or working
"AND",
["custrecord_stc_queue_element_type","anyof","1"] // sales order
],
columns:
[
'internalid'
]
});
return queueElementSearchObj;
}
reduce = (context) => {
log.debug('context', context);
var queueElementRecord = null;
var error = null;
try {
// load the process request custom record
queueElementRecord = record.load({
type: 'customrecord_stc_queue_element',
id: context.key,
isDynamic: true
});
var payload = queueElementRecord.getValue('custrecord_stc_queue_element_payload');
payload = JSON.parse(payload);
/////////////////////////////////////////////////////////
// CREATE YOUR OWN FUNCTION HERE TO PROCESS THE PAYLOAD
/////////////////////////////////////////////////////////
var salesOrderId = createSalesOrder(payload);
////////////////////////////////////////////////////////
queueElementRecord.setValue('custrecord_stc_queue_element_date_proc', new Date());
queueElementRecord.setValue('custrecord_stc_queue_element_transaction', salesOrderId);
queueElementRecord.setValue('custrecord_stc_queue_element_error', '');
queueElementRecord.setValue('custrecord_stc_queue_element_next_retry', '');
queueElementRecord.setValue('custrecord_stc_queue_element_num_retries', '');
queueElementRecord.setValue('custrecord_stc_queue_element_status', '3'); // completed
queueElementRecord.save();
return;
}
catch(e) {
error = JSON.stringify(e);
log.error('error', error);
}
try {
// if an error occurred and there is a valid queueElementRecord
if (error && queueElementRecord) {
queueElementRecord.setValue('custrecord_stc_queue_element_error', error);
var numRetries = queueElementRecord.getValue('custrecord_stc_queue_element_num_retries');
var scriptObj = runtime.getCurrentScript();
var maxRetries = scriptObj.getParameter({name: 'custscript_max_retries'});
// if we are at maxRetries or greater, mark the record as failed and do not retry
if (numRetries+1 >= maxRetries) {
queueElementRecord.setValue('custrecord_stc_queue_element_status', 4); // failed
queueElementRecord.setValue('custrecord_stc_queue_element_next_retry', '');
}
else {
// otherwise set the the status to working and set next retry date
queueElementRecord.setValue('custrecord_stc_queue_element_status', 2); // working
// Create your own custom retry logic here as needed
// As an example, next retry date is next retry date + numRetries+1 hours
var nextRetryDate = new Date();
var dateToMilliseconds = nextRetryDate.getTime();
var addedHours = dateToMilliseconds + (3600000*(numRetries+1));
var newDate = new Date(addedHours);
queueElementRecord.setValue('custrecord_stc_queue_element_next_retry', newDate);
}
queueElementRecord.setValue('custrecord_stc_queue_element_num_retries', numRetries+1);
queueElementRecord.save();
}
}
catch(e) {
log.error('Error trying to save record in errored state', JSON.stringify(e));
}
}
createSalesOrder = (payload) => {
var salesOrderRecord = record.create({
type: record.Type.SALES_ORDER,
isDynamic: true
});
salesOrderRecord.setValue('entity', payload.customerid);
for (let i = 0; i < payload.items.length; i++) {
var payloadItem = payload.items[i];
var lineNum = salesOrderRecord.selectNewLine({sublistId: 'item'});
salesOrderRecord.setCurrentSublistValue({sublistId: 'item',fieldId: 'item',value: payloadItem.item});
salesOrderRecord.setCurrentSublistValue({sublistId: 'item',fieldId: 'quantity',value: payloadItem.quantity});
salesOrderRecord.setCurrentSublistValue({sublistId: 'item',fieldId: 'rate',value: payloadItem.rate});
salesOrderRecord.commitLine({sublistId: 'item'});
}
var recordId = salesOrderRecord.save();
return recordId;
}
summarize = (summary) => {
log.debug('==END==','==END==');
}
return {
getInputData: getInputData,
reduce: reduce,
summarize: summarize
};
});
Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.
We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!
It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –