Requirement
On the Item Receipt record, we can use customization to source the landed cost category values at the item receipt line level. we need to source the landed cost category values from the landed cost template to the standard landed cost golden box by using customization.
Solution
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {
/**
* Defines the function definition that is executed before record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const beforeSubmit = (scriptContext) => {
try {
let itemReceipt = scriptContext.newRecord
let createdFrom = scriptContext.newRecord.getValue({fieldId: 'createdfrom'})
let IRCreatedForm = checkCreatedForm(createdFrom)
if (IRCreatedForm) {
let itemReceiptSublistLineCount = itemReceipt.getLineCount({
sublistId: 'item'
})
for (let i = 0; i < itemReceiptSublistLineCount; i++) {
log.debug('for loop itemReceiptSublistLineCount . i..', i)
let itemreceive = itemReceipt.getSublistValue({
sublistId: 'item',
fieldId: 'itemreceive',
line: i
})
log.debug('itemreceive', itemreceive)
if (itemreceive == true) {
let temp = itemReceipt.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_lc_template_peritem',
line: i
})
log.debug('temp', temp)
if (checkForParameter(temp)) {
let quantity = itemReceipt.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
})
let rate = itemReceipt.getSublistValue({
sublistId: 'item',
fieldId: 'rate',
line: i
})
let calculate = itemReceipt.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_scm_lc_autocalc',
line: i
})
log.audit('calculate', calculate)
let land = fetchTemplateValue(temp)
if (land.length>0) {
let landedCostCount = land.length
let landedCostArray = getValueFromTemp(land, landedCostCount)
let subrecord = itemReceipt.getSublistSubrecord({
sublistId: 'item',
fieldId: 'landedcost',
line: i
});
let subRecSublistCount = subrecord.getLineCount({
sublistId: 'landedcostdata',
})
for (let ss = 0; ss < landedCostArray.length; ss++) {
let costFact
if (landedCostArray[ss].costCategory == '4') {
costFact = (landedCostArray[ss].costFactor * quantity)
} else if (landedCostArray[ss].costCategory == '5') {
costFact = (landedCostArray[ss].costFactor * quantity * rate)
} else if (landedCostArray[ss].costCategory == '6') {
costFact = (landedCostArray[ss].costFactor * quantity)
}
subrecord.setSublistValue({
sublistId: 'landedcostdata',
fieldId: 'costcategory',
value: landedCostArray[ss].costCategory,
line: ss,
});
subrecord.setSublistValue({
sublistId: 'landedcostdata',
fieldId: 'amount',
line: ss,
value: costFact
})
}
itemReceipt.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_landedcost_appld_joinf1299',
line: i,
value: true
})
}
}
}
}
}
} catch (e) {
log.debug('error@beforeSubmit', e)
}
}
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
}
function fetchTemplateValue(id) {
try {
var templateValueSearchObj = search.create({
type: "customrecord_scm_lc_profile",
filters:
[
["internalid", "anyof", id]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({
name: "custrecord_scm_lc_dtl_costcat",
join: "CUSTRECORD_SCM_LC_DTL_PROFILE",
label: "Cost Category"
}),
search.createColumn({
name: "custrecord_scm_lc_dtl_factor",
join: "CUSTRECORD_SCM_LC_DTL_PROFILE",
label: "Cost Factor"
})
]
});
let savedSearchRes = templateValueSearchObj.run().getRange({
start: 0,
end: 1000
})
if (savedSearchRes.length > 0) {
return savedSearchRes
} else {
return []
}
} catch (e) {
log.debug('error@fetchTemplateValue', e)
}
}
function checkCreatedForm(id) {
try {
var transactionSearchObj = search.create({
type: "transaction",
filters:
[
["internalid", "anyof", id],
"AND",
["formulanumeric: CASE WHEN {type}='Purchase Order' THEN 1 ELSE 0 END", "equalto", "1"],
"AND",
["mainline", "is", "T"]
],
columns:
[
search.createColumn({name: "type", label: "Type"})
]
});
let savedSearchRes = transactionSearchObj.run().getRange({
start: 0,
end: 1
})
if (savedSearchRes.length > 0) {
return true
} else {
return false
}
} catch (e) {
log.debug('error@checkCreatedForm', e)
}
}
function getValueFromTemp(land, landedCostCount) {
try {
let landedCostArrays = []
for (let l = 0; l < landedCostCount; l++) {
let landedCostTemp = {}
landedCostTemp.costCategory = land[l].getValue({
name: "custrecord_scm_lc_dtl_costcat",
join: "CUSTRECORD_SCM_LC_DTL_PROFILE",
label: "Cost Category"
})
landedCostTemp.costFactor = land[l].getValue({
name: "custrecord_scm_lc_dtl_factor",
join: "CUSTRECORD_SCM_LC_DTL_PROFILE",
label: "Cost Factor"
})
if (landedCostTemp.costFactor > 0) {
landedCostArrays.push(landedCostTemp)
}
}
return landedCostArrays
} catch (e) {
log.debug('error@getValueFromTemp', e)
}
}
function checkForParameter(parameter) {
if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
return true;
}
}
return {beforeSubmit}
});