I've created a user event script before load (on Task form) which should insert the sublist values for multiple fields (custom sublist child record) on the task form. Sources by the "Transaction" field.
I would like to know how I can loop through the lines on the sales order and then create these lines in the custom child record.
This is what I have - This is working. But im missing the loop function and I do now understand it.
Would be pleased if someone can help:
update:
I found a way to loop the sublist. But im not able to use the function "Commit Line" to set the next line on the custom sublist. Does someone know how to do that?
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(['N/record'],
function(record) {
function beforeLoad(context) {
if (context.type !== context.UserEventType.CREATE)
return;
var newRecord = context.newRecord;
var objRecord = record.load({
type: record.Type.SALES_ORDER,
id: context.newRecord.getValue({fieldId: 'transaction'}),
isDynamic: true
});
var objSublist = objRecord.getSublist({
sublistId: 'item'
});
var numLines = objRecord.getLineCount({
sublistId: 'item'
});
for (var line = 0; line < numLines.length; line++)
var objColumn = objSublist.getColumn({
fieldId: 'item'
});
var ObjField = objRecord.getSublistField({
sublistId: 'item',
fieldId: 'custcol12',
line: line
});
var sublistFieldValue = objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol12',
line: line,
});
//SET Position#
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord251',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol12',
line: line})
});
//SET Item
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord252',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: line})
});
//SET Description
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord253',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'description',
line: line})
});
//SET QUANTITY
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord257',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: line})
});
//SET INVENTORY DETAILS
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord254',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'inventorydetail',
line: line})
});
//SET Condition
newRecord.setSublistValue({
sublistId: 'recmachcustrecord250',
fieldId: 'custrecord255',
line: line,
value: objRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol29',
line: line})
});
}
return {
beforeLoad: beforeLoad,
};
});