FAgudelo
07/09/2025, 5:46 PMFAgudelo
07/09/2025, 5:46 PM* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define(['N/search', 'N/record', 'N/log'], function (search, record, log) {
/**
* Retrieves Inventory Items that meet the specified inactivation criteria.
*/
function getInputData() {
return search.create({
type: search.Type.INVENTORY_ITEM,
filters: [
["type", "anyof", "InvtPart"],
"AND",
["transaction.type", "anyof", "@NONE@"],
"AND",
["locationquantityonhand", "isempty", ""],
"AND",
["created","within","3/24/2024 12:00 am","3/31/2024 11:59 pm"]
],
columns: [
'internalid',
'itemid'
]
});
}
/**
* Inactivates each Inventory Item retrieved by the search.
*/
function map(context) {
try {
var result = JSON.parse(context.value);
var itemId = result.id;
var itemRecord = record.load({
type: record.Type.INVENTORY_ITEM,
id: itemId,
isDynamic: false
});
itemRecord.setValue({
fieldId: 'isinactive',
value: true
});
var savedId = itemRecord.save();
log.debug({
title: 'Item Inactivated',
details: 'Item ID: ' + itemId + ', Saved As: ' + savedId
});
} catch (e) {
log.error({
title: 'Error Inactivating Item',
details: e.message || JSON.stringify(e)
});
}
}
return {
getInputData: getInputData,
map: map
};
});
ehcanadian
07/09/2025, 6:11 PMFAgudelo
07/09/2025, 6:40 PMMike Robbins
07/09/2025, 6:42 PMTim Chapman
07/09/2025, 6:56 PMShannon
07/09/2025, 10:59 PMFAgudelo
07/09/2025, 11:01 PMjen
07/09/2025, 11:11 PMNElliott
07/10/2025, 7:50 AMShawn Talbert
07/10/2025, 4:45 PMehcanadian
07/10/2025, 5:31 PMFAgudelo
07/10/2025, 7:09 PM/**
* @NApiVersion 2.x
* @NScriptType MapReduceScript
*/
define(['N/file', 'N/search', 'N/record', 'N/log'], function(file, search, record, log) {
const SOURCE_FOLDER_ID = 65164;
const DONE_FOLDER_ID = 65165;
function getInputData() {
var allItemIds = [];
var fileSearchResults = search.create({
type: 'file',
filters: [
['folder', 'anyof', SOURCE_FOLDER_ID],
'AND',
['filetype', 'is', 'CSV']
],
columns: ['internalid']
}).run().getRange({ start: 0, end: 1000 });
for (var i = 0; i < fileSearchResults.length; i++) {
var fileId = fileSearchResults[i].getValue({ name: 'internalid' });
try {
var fileObj = file.load({ id: fileId });
var contents = fileObj.getContents();
var lines = contents.split(/\r?\n/);
for (var j = 1; j < lines.length; j++) { // Skip header
var line = lines[j].trim();
if (!line) continue;
var internalId = line.split(',')[0].trim();
if (internalId && !isNaN(internalId)) {
allItemIds.push(internalId);
} else {
log.audit('Skipped Line', 'Line ' + (j + 1) + ' is invalid: "' + line + '"');
}
}
// Move processed file to Done folder
fileObj.folder = DONE_FOLDER_ID;
fileObj.save();
log.audit('File Processed', 'Processed file ID: ' + fileId + ' and moved to Done folder.');
} catch (fileError) {
log.error('File Load Failed', 'Could not process file ID ' + fileId + ': ' + fileError.message);
}
}
return allItemIds;
}
function map(context) {
var itemId = context.value;
try {
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: itemId,
values: {
isinactive: true
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
log.audit('Item Inactivated', 'Item ID ' + itemId + ' set to inactive.');
} catch (e) {
log.error('Error Inactivating Item', 'Item ID ' + itemId + ': ' + e.message);
}
}
return {
getInputData: getInputData,
map: map
};
});
Shawn Talbert
07/10/2025, 7:40 PMmap
and reduce
text at the beginning of the article?ehcanadian
07/11/2025, 2:47 AMShawn Talbert
07/14/2025, 4:56 AM