Requirement
The client needs to close the transfer orders and the RMA records automatically which is not scheduled and created 10 days ago.
Solution
A scheduled script can be used to close the transfer orders and RMS records which is not scheduled and created 10 days ago.
For the Transfer and RMA orders both ignore the “Type of Delivery” and criteria will be
- If DT order # exist then skip it
- If received then skip it
If both conditions are met, the records can be closed.
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
*/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
const getInputData = (inputContext) => {
try {
log.debug("in getInputData")
let searchFilterArray = searchMainToApplyFilters()
let resultDataArray = []
for (let key in searchFilterArray) {
let fetchTransactionDetailsSearchResult = fetchTransactionDetailsSearch(searchFilterArray[key])
if (checkForParameter(fetchTransactionDetailsSearchResult)) {
if (fetchTransactionDetailsSearchResult.length > 0) {
resultDataArray.push(...fetchTransactionDetailsSearchResult)
}
}
}
return resultDataArray
} catch (e) {
log.debug("error@getInputData", e)
}
}
const reduce = (reduceContext) => {
try {
let dataObj = JSON.parse(reduceContext.values[0])
log.debug("dataObj..", dataObj)
if (checkForParameter(dataObj.recordtype) && checkForParameter(dataObj.internalid)) {
let transactionRecord = record.load({
type: dataObj.recordtype,
id: dataObj.internalid
})
let itemLineCount = transactionRecord.getLineCount({
sublistId: "item"
})
if ((transactionRecord.type == "transferorder") && (transactionRecord.getValue({
fieldId: "customform"
}) != 57)) {
transactionRecord.setValue({
fieldId: "customform",
value: 57
})
}
if (itemLineCount > 0) {
for (let itemLineNo = 0; itemLineNo < itemLineCount; itemLineNo++) {
transactionRecord.setSublistValue({
sublistId: "item",
fieldId: "isclosed",
value: true,
line: itemLineNo
})
}
}
let id = transactionRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
})
log.debug("id....", id)
}
} catch (e) {
log.debug("error@getInputData", e)
}
}
const summarize = (summaryContext) => {
}
function fetchTransactionDetailsSearch(transObject) {
try {
var transactionSearchObj = search.create({
type: transObject.type,
filters:
[
["custcol_dt_order", "isempty", ""],
"AND",
["mainline", "is", "F"],
"AND",
["datecreated","onorbefore","tendaysago"],
"AND",
[...transObject.filter]
],
columns:
[
search.createColumn({
name: "internalid",
summary: "GROUP",
label: "Internal ID"
}),
search.createColumn({
name: "recordtype",
summary: "GROUP",
label: "Record Type"
})
]
});
let searchRes = transactionSearchObj.run().getRange({
start: 0,
end: 1000
})
let transactionDetArray = []
if (searchRes.length > 0) {
for (let toCount = 0; toCount < searchRes.length; toCount++) {
let transactionDetObject = {}
for (let column = 0; column < transactionSearchObj.columns.length; column++) {
transactionDetObject[transactionSearchObj.columns[column].name] = searchRes[toCount].getValue(searchRes[toCount].columns[column])
}
transactionDetArray.push(transactionDetObject)
}
return transactionDetArray
} else {
return []
}
} catch (e) {
log.debug("error@fetchTransactionDetailsSearch", e)
}
}
/**
* The function is to create an object with the search criteria for transfer order and return authorization
* @returns {{}}- returns the searchObject
*/
function searchMainToApplyFilters() {
try {
let mainSearchObj = {}
mainSearchObj.searchObj1 = {
type: "returnauthorization",
filter: [
["type", "anyof", "RtnAuth"],
"AND",
["status", "anyof", "RtnAuth:A", "RtnAuth:B", "RtnAuth:C"],
"AND",
["internalid", "anyof", "199273"]
]
}
mainSearchObj.searchObj2 = {
type: "transferOrder",
filter: [["type", "anyof", "TrnfrOrd"],
"AND",
["status", "anyof", "TrnfrOrd:A", "TrnfrOrd:C", "TrnfrOrd:B"],
"AND",
["internalid", "anyof", "170943"]
]
}
return mainSearchObj
} catch (e) {
log.debug("error@searchMainToApplyFilters", e)
}
}
function checkForParameter(parameter) {
if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
return true;
}
}
return {getInputData, reduce, summarize}
});