Map reduce script that will process all Vendor RFQ record lines that had a response entered in the previous day. Manufacturer record is a custom record.
define(['N/search', 'N/record', 'N/runtime'], function (search, record, runtime) {
/**
* @description Function to get authorized vendor id
* @param {*} manufacturerId
* @returns number
*/
function authorizedVendor(manufacturerId) {
try {
let customrecord_mhi_ibs_manufacturersSearchObj = search.create({
type: "customrecord_mhi_ibs_manufacturers",
filters:
[
["internalid", "anyof", manufacturerId]
],
columns:
[
search.createColumn({ name: "name", label: "Name" }),
search.createColumn({ name: "id", label: "ID" }),
search.createColumn({ name: "custrecord_mhi_ibs_authorized_vendors", label: "Authorized Vendor" })
]
});
let manId;
customrecord_mhi_ibs_manufacturersSearchObj.run().each(function (result) {
manId = result.getValue({
name: "custrecord_mhi_ibs_authorized_vendors"
})
});
return manId
} catch (e) {
log.error("error@authorizedVendor", e)
}
}
/**
* @description Function to get authorized vendor id
* @param {*} manufacturerId
* @returns number
*/
function unauthorizedVendor(manufacturerId) {
try {
let customrecord_mhi_ibs_manufacturersSearchObj = search.create({
type: "customrecord_mhi_ibs_manufacturers",
filters:
[
["internalid", "anyof", manufacturerId]
],
columns:
[
search.createColumn({ name: "name", label: "Name" }),
search.createColumn({ name: "id", label: "ID" }),
search.createColumn({ name: "custrecord_mhi_ibs_unauthorized_vend", label: "Unauthorized Vendor" })
]
});
let manId;
customrecord_mhi_ibs_manufacturersSearchObj.run().each(function (result) {
manId = result.getValue({
name: "custrecord_mhi_ibs_unauthorized_vend"
})
});
return manId
} catch (e) {
log.error("error@unauthorizedVendor", e)
}
}
/**
* Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
* @returns {Array} The input data to use in the map/reduce process
*/
function getInputData() {
try {
const vendorRFQSearch = search.create({
type: "transaction",
settings: [{ "name": "consolidationtype", "value": "ACCTTYPE" }, { "name": "includeperiodendtransactions", "value": "F" }],
filters:
[
["type", "anyof", "Custom101"],
"AND",
["custcol_mhi_ibs_response_date", "on", "yesterday"],
],
columns:
[
search.createColumn({
name: "category",
join: "vendor",
label: "Category"
}),
search.createColumn({ name: "custbody_mhi_ibs_vendor", label: "Vendor *" }),
search.createColumn({ name: "custcol_mhi_ibs_manufacturers", label: "Manufacturer**" }),
search.createColumn({ name: "tranid", label: "Document Number" })
]
});
//log.debug("vendorRFQSearch", vendorRFQSearch)
return vendorRFQSearch;
} catch (e) {
log.error("error@getInputData", e)
}
}
/**
* Defines the function that is executed when the map entry point is triggered.
* @param {Object} context
*/
function map(context) {
try {
const searchResult = JSON.parse(context.value);
//log.debug("searchResult", searchResult)
const rfqId = searchResult.id;
const vendorId = searchResult.values.custbody_mhi_ibs_vendor.value;
const manufacturerId = searchResult.values.custcol_mhi_ibs_manufacturers.value;
let vendorAuthorized, vendorUnauthorized;
let vendorRecord = search.lookupFields({
type: search.Type.VENDOR,
id: vendorId,
columns: ['category']
});
const vendorCategory = vendorRecord.category;
if (vendorCategory[0].value == 5 || vendorCategory[0].value == 7) {
if (manufacturerId) {
vendorAuthorized = authorizedVendor(manufacturerId)
let stringValues = vendorAuthorized ? vendorAuthorized.split(",") : [];
for (let i = 0; i < stringValues.length; i++) {
if (stringValues[i] == vendorId) {
return;
}
}
stringValues.push(vendorId)
record.submitFields({
type: 'customrecord_mhi_ibs_manufacturers',
id: manufacturerId,
values: {
'custrecord_mhi_ibs_authorized_vendors': stringValues
}
});
}
} else if (vendorCategory[0].value == 8 || vendorCategory[0].value == 6) {
if (manufacturerId) {
vendorUnauthorized = unauthorizedVendor(manufacturerId)
let stringValues = vendorUnauthorized ? vendorUnauthorized.split(",") : [];
for (let i = 0; i < stringValues.length; i++) {
if (stringValues[i] == vendorId) {
return;
}
}
stringValues.push(vendorId)
record.submitFields({
type: 'customrecord_mhi_ibs_manufacturers',
id: manufacturerId,
values: {
'custrecord_mhi_ibs_unauthorized_vend': stringValues
}
});
}
} else {
return;
}
} catch (e) {
log.error("error@map", e)
}
}
function reduce(context) {
// Reduce is not needed for this script
}
/**
* Defines the function that is executed when the summarize entry point is triggered.
* @param {Object} summary
*/
function summarize(summary) {
log.audit('Script Summary', `Total Records Processed: ${summary.inputSummary.recordCount}`);
summary.mapSummary.errors.iterator().each(function (key, error) {
log.error('Map Error', `Key: ${key}, Error: ${error}`);
return true;
});
}
return {
getInputData: getInputData,
map: map,
reduce: reduce,
summarize: summarize
};
});