I am trying to import UOMs into NetSuite using a map/reduce script since we have over 4,600 that need to be setup. I've made good progress to this point, but I am completely stymied by the first question below and I am going to go ahead and ask a second question that I will have to deal with later.
Question 1: I keep getting the error "Conversion rate must be greater than zero." I have even gone so far in the script to hard code a value of 1 to try and get around this and still get the error. If I interrogate the sublist value for conversionrate, it shows that it is set to 1. Any thoughts?
Question 2: Right now, I am hardcoding the line number value on the sublist just trying to get a single record to create, but how do I handle this when I import the master file that may have several sublist values for a given Unit Type? In something like a scheduled script, it is easy to just iterate through, but I am not sure how to handle that in map reduce since the map stage is only ever dealing with one row of data. Can it be done in reduce by using the key value and incrementing the line number in a traditional loop (i.e., are all of the rows accessible at once in reduce, or am I still dealing with a single key/value pair)?
Here is a sample row of data from the CSV file used for importing (ignore the Sort Col, it is not mapping to anything):
Name, Unit Name, Plural Name, Abbreviation, Plural Abbreviation, Conversion Rate, Base Unit, Sort Col
BD > 3 EA/BX > 3 EA/CT,EA,EA,EA,EA,1,Yes,BD > 000003 EA/BX > 000003 EA/CT1
Here is my script:
/**
*
* @NScriptName UOM Load
* @NScriptType MapReduceScript
* @NApiVersion 2.x
*/
define(["N/email", "N/log", "N/record", "N/file"], function (email, log, record, file) {
function getInputData(){
log.debug('getInputData', "getInputData...");
try {
var csvLines = file.load ({
id: 79470
}).getContents().split(/\n|\n\r/);
log.debug('getInputData',"CSV Lines: " + csvLines.length);
return csvLines;
}
catch(e){
log.error('getInputData', e.message);
}
};
function map(context){
try {
log.debug('map', "Map Start: " + context.value);
var currRow = JSON.parse('"' + context.value + '"');
log.debug('map', "After Parse...")
var currRowContent = currRow.split(',');
log.debug('map', "After Split...")
var unitsTypeRecord = record.create({
type: record.Type.UNITS_TYPE
});
log.debug('unitsTypeRecord',"sublists: " + unitsTypeRecord.getSublists());
log.debug('unitsTypeRecord',"sublist fields: " + unitsTypeRecord.getSublistFields({sublistId: 'uom'}));
unitsTypeRecord.setValue({fieldId: 'name', value: currRowContent[0]});
log.debug('unitsTypeRecord',"name: " + unitsTypeRecord.getValue({fieldId: 'name'}));
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'unitName', value: currRowContent[1], line: 0});
log.debug('unitsTypeRecord',"unitname: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'unitName', line: 0}));
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'pluralName', value: currRowContent[2], line: 0});
log.debug('unitsTypeRecord',"pluralname: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'pluralName', line: 0}));
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'abbreviation', value: currRowContent[3], line: 0});
log.debug('unitsTypeRecord',"abbreviation: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'abbreviation', line: 0}));
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'pluralAbbreviation', value: currRowContent[4], line: 0});
log.debug('unitsTypeRecord',"pluralabbreviation: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'pluralAbbreviation', line: 0}));
log.debug('currRowcontent',"currRowContent[5]: " + Number('1'));
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'conversionRate', value: Number('1'), line: 0});
log.debug('unitsTypeRecord',"conversionrate: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'conversionRate', line: 0}));
if (currRowContent[6] == 'Yes') {
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'baseUnit', value: true, line: 0})}
else {
unitsTypeRecord.setSublistValue({sublistId: 'uom', fieldId: 'baseUnit', value: false, line: 0})
};
log.debug('unitsTypeRecord',"baseunit: " + unitsTypeRecord.getSublistValue({sublistId: 'uom', fieldId: 'baseUnit', line: 0}));
unitsTypeRecord.save();
}
catch(e){
log.error('map', e.message);
}
};
function summarize(){
log.debug('summarize', "summarize...");
try {
}
catch(e){
log.error('summarize', e.message);
}
};
return {
getInputData: getInputData,
map: map,
summarize: summarize
};
});