Update Line Field of purchase order on updating the body field

Client would like to implement the following field update in the purchase order record

  • Receive By date field in the purchase order will be update by UI/CSV import/SOAP web services in the Purchase order record
  • There is a line column field “Expected Receipt Date” in the purchase order
  • For every update in the “Receive By ” date, the “Expected Receipt date” at each item level need to be update with this value in a purchase order

We will be implementing a userevent script in the purchase order so that any update in the create and edit mode of the record will be considered in the script. Whenever the value in the “receive By” date field is changed, the change will be reflected in the line level after submitting the record.

Code

/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record'],
/**
* @param{record} record
*/
(record) => {
const beforeLoad = (scriptContext) => {
try {
if (scriptContext.type === scriptContext.UserEventType.COPY) {
const purchaseOrder = scriptContext.newRecord;
let lineCount = purchaseOrder.getLineCount({
sublistId: "item",
})
//loop through number of lines in PO
if(lineCount > 0){
for (let i = 0; i < lineCount; i++) {
purchaseOrder.setSublistValue({
sublistId: "item",
fieldId: "expectedreceiptdate",
line: i,
value: " "
});
}
}
}
} catch (e){
log.debug("error@beforeLoad",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) => {
try {
if (scriptContext.type === scriptContext.UserEventType.CREATE ||
scriptContext.type === scriptContext.UserEventType.EDIT) {
var newRecord = scriptContext.newRecord
var recordId = scriptContext.newRecord.id
var purchaseOrder = record.load({
type: record.Type.PURCHASE_ORDER,
id: recordId,
isDynamic: true
})
var receiveByDate = purchaseOrder.getValue({
fieldId: 'duedate'
})
var lineCount = purchaseOrder.getLineCount({
sublistId: "item",
})
//loop through number of lines in PO
if(lineCount > 0){
for (let i = 0; i < lineCount; i++) {
purchaseOrder.selectLine({
sublistId: 'item',
line: i
});
purchaseOrder.setCurrentSublistValue({
sublistId: "item",
fieldId: "expectedreceiptdate",
value: new Date(receiveByDate)
});
purchaseOrder.commitLine({
sublistId : 'item',
});
}
}
purchaseOrder.save();
}
} catch (error) {
log.debug("Error on beforeSubmit", error);
}
}
return { beforeLoad, afterSubmit}
});

Leave a comment

Your email address will not be published. Required fields are marked *