For a badge here we have a start date and end date badges expired and badges scheduled for future we need to set only the badges which available using this Methode we can set values accordingly
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(["N/search", "N/log", "N/record"], function (search, log, record) {
function getInputData() {
var verifyedBadges={};
var customrecord_ns_ib_badgesSearchObj = search.create({
type: "customrecord_ns_ib_badges",
filters:
[
["custrecord_item.type","anyof","InvtPart"]
],
columns:
[
search.createColumn({
name: "name",
sort: search.Sort.ASC,
label: "Name"
}),
search.createColumn({
name: "type",
join: "CUSTRECORD_ITEM",
label: "Type"
}),
search.createColumn({
name: "internalid",
join: "CUSTRECORD_ITEM",
label: "Internal ID"
}),
search.createColumn({name: "custrecordstart_date", label: "Start-date"}),
search.createColumn({name: "custrecord_end_date", label: "End-Date"}),
search.createColumn({name: "internalid", label: "Internal ID"})
]
});
var searchResultCount = customrecord_ns_ib_badgesSearchObj.runPaged().count;
log.debug("customrecord_ns_ib_badgesSearchObj result count",searchResultCount);
customrecord_ns_ib_badgesSearchObj.run().each(function(result){
var itemId= result.getValue(customrecord_ns_ib_badgesSearchObj.columns[2]);
var startdate= result.getValue(customrecord_ns_ib_badgesSearchObj.columns[3]);
var enddate= result.getValue(customrecord_ns_ib_badgesSearchObj.columns[4]);
var currentDate=(new Date());
startdate=startdate? new Date(startdate):currentDate;
enddate=enddate? new Date(enddate):currentDate;
if (!verifyedBadges[itemId]) {
if ((startdate<=currentDate)&&(currentDate<=enddate)) {
verifyedBadges[itemId]=[result.getValue({name: "internalid", label: "Internal ID"})];
}
else{
verifyedBadges[itemId] = [];
}
}
else if ((startdate<=currentDate)&&(currentDate<=enddate)) {
verifyedBadges[itemId].push(result.getValue({name: "internalid", label: "Internal ID"}))
}
return true;
});
log.debug("verifyedBadges",verifyedBadges)
return verifyedBadges;
}
function reduce(context) {
try {
let newBadge=JSON.parse(context.values);
log.debug("context.values", JSON.parse(context.values))
var objRecord=record.load({
type: "inventoryitem",
id: context.key,
})
var existBadge=objRecord.getValue({ fieldId: "custitem_ns_ib_badges" })
log.debug("cexistBadge", (existBadge))
var commonItem=existBadge.filter(value => newBadge.includes(value))
log.debug("cexicommonItemstBadge", commonItem)
objRecord.setValue({
fieldId: "custitem_ns_ib_badges",
value: commonItem,
ignoreFieldChange: true,
});
let itemId = objRecord.save({ignoreMandatoryFields: true});
log.debug("itemId", itemId)
} catch (error) {
log.error("error@reduce",error)
}
}
return {
getInputData: getInputData,
reduce: reduce
};
});