Need a little help. I was asked to disable the lin...
# suitescript
m
Need a little help. I was asked to disable the line amount field on Sales Orders so people can’t change the total amount. i have a client script that is working, but only after the line is committed. When you go back to the line the field is disabled, but it doesn’t prevent it when they are first entering a new item line. Is there another way or should I just add some code to recalculate the amount (qty x rate) when the line is committed and do I use a different client entry function for this? Here’s the section of code I have so far.
function lineInit(context) {
try {
/** this will disable the amount for existing line */
var rec = context.currentRecord;
var itemCount = rec.getLineCount('item');
var sublistName = context.sublistId;
var line = rec.getCurrentSublistIndex({ sublistId: sublistName });
if (itemCount >= line) {
var theField = rec.getSublistField({
sublistId: sublistName,
fieldId: 'amount',
line: line
});
theField.isDisabled = true;
}
} catch (e) {
console.error(':octagonal_sign: lineInit', JSON.stringify(e, null, 4));
}
}
b
you also typically have to use pageInit, lineInit is only triggered only when a line is selected, and the first line on the item sublist is already selected
m
I tried Column.isDisabled on pageInit. The colum was disabled at first, but as soon as I entered an item, it changed back to editable.
Would I also have to do something like this on fieldChanged?
b
postSourcing, but yes, netsuite undisables a field if it uses it for sourcing
m
OK. I’ll try that. getting closer though. Thanks!
Finally, it worked
function postSourcing(context) {
var myRec = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item' && sublistFieldName === 'item'){
var sublistObj = myRec.getSublist({
sublistId: 'item'
});
var columnObj = sublistObj.getColumn({
fieldId: 'amount'
});
//set
columnObj.isDisabled = true;
}
}
Thanks @battk
j
I had the same need, and found this just in time to help me out. Thanks!
m
Here’s my whole script. It actually includes Pageinit, Post Sourcing, and Line Init. All three are necessary so if the person goes back to the line, they can’t edit the total field in any circumstance.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/record'],
/**
* @param{record} record
*/
function(record) {
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function pageInit(context) {
var rec = context.currentRecord;
var sublistObj = rec.getSublist({
sublistId: 'item'
});
var columnObj = sublistObj.getColumn({
fieldId: 'amount'
});
//set
columnObj.isDisabled = true;
//get
var isDisabled = columnObj.isDisabled;
}
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(context) {
}
/**
* Function to be executed when field is slaved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
*
* @since 2015.2
*/
function postSourcing(context) {
var myRec = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item' && sublistFieldName === 'item'){
var sublistObj = myRec.getSublist({
sublistId: 'item'
});
var columnObj = sublistObj.getColumn({
fieldId: 'amount'
});
//set
columnObj.isDisabled = true;
}
}
/**
* Function to be executed after sublist is inserted, removed, or edited.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @since 2015.2
*/
function sublistChanged(scriptContext) {
}
/**
* Function to be executed after line is selected.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @since 2015.2
*/
function lineInit(context) {
try {
/** this will disable the amount for existing line */
var rec = context.currentRecord;
var itemCount = rec.getLineCount('item');
log.debug({
title: itemCount
});
var sublistName = context.sublistId;
var line = rec.getCurrentSublistIndex({ sublistId: sublistName });
if (itemCount >= 0 || itemCount == null) {
log.debug({
title: "line created"
})
var theField = rec.getSublistField({
sublistId: sublistName,
fieldId: 'amount',
line: line
});
log.debug({
title: "something"
});
theField.isDisabled = true;
}
} catch (e) {
console.error(':octagonal_sign: lineInit', JSON.stringify(e, null, 4));
}
}
/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {
}
/**
* Validation function to be executed when sublist line is committed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateLine(scriptContext) {
}
/**
* Validation function to be executed when sublist line is inserted.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateInsert(scriptContext) {
}
/**
* Validation function to be executed when record is deleted.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateDelete(scriptContext) {
}
/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {
}
return {
postSourcing: postSourcing,
lineInit: lineInit,
pageInit: pageInit
};
});