How do I dynamically add select options to a custo...
# suitescript
n
How do I dynamically add select options to a custom select fields via client script? I tried to do this:
Copy code
var sublist = currentRec.getSublist({sublistId: sublistId});
var addrCol = sublistId.getColumn({fieldId: 'custpage_addresses_col'});
but addrCol does not have a function to add select options. Also, I am doing this in fieldChanged event but the doc says CurrentRecord.getSublistField(options) is not valid in fieldChanged events. Any help?
b
CurrentRecord.getSublistField is what you should use
though its line requirement is pretty annoying
n
Yes, that won't work for me because I am in fieldChanged event.
Should I get the line from getCurrentSublistIndex?
b
if there are already lines, then usually you can just grab the first line
if there are no existing lines, there are no good options
n
This will not help because the options can change depending on a specific field value.
It's annoying that there is no such option in SS 2.x as compared to SS 1.0
b
what would you do in ss1?
n
nlapiInsertLineItemOptions() - simple.
b
you can still use that in your ss2 client script
n
yes, that's what I did.
b
its probably better than the ss2 alternatives
which involve getting the field ahead of time and storing it for future use
n
yes, but what surprises me is that there is no simple alternative in SS2
b
or using a bug around the handling of NaN
n
what's that?
b
the code that throws an error when you pass an invalid line looks like:
Copy code
function validateAndGetOneBasedIndex(index, method, indexUpperLimit)
	    {
	        if (isNaN(index))
	            return index;
	        else
	            index = parseInt(index, 10);

	        if (index < 0 || (indexUpperLimit !== undefined && index >= indexUpperLimit))
	        {
	            throw error.create({name: error.Type.INVALID_SUBLIST_OPERATION, message: method});
	        }
	        else
	        {
	            return index + 1;
	        }
	    }
the code for getSublistField looks like
Copy code
function getSublistField(options, fieldId, line)
		{
			var sublistId,
				undef = undefined;

			if (fieldId !== undef && line !== undef)
			{
				sublistId = options;
			}
			else if (options !== undef && options !== null)
			{
				sublistId = options.sublistId;
				fieldId = options.fieldId;
				line = options.line;
			}
			utilityFunctions.checkArgs([sublistId, fieldId, line], ['sublistId', 'fieldId', 'line'], getMissingArgumentErrorMessageFillerValue('getSublistField'));
			line = recordUtil.validateAndGetOneBasedIndex(line, "CurrentRecord.getSublistField", nlapiGetLineItemCount(sublistId));

			var fldObj = nlapiGetLineItemField(sublistId, fieldId, line);
			if (fldObj && fldObj.type === fieldTypeConstants.Type.TEXT && getEncodedFieldType(sublistId, fieldId, true) === fieldTypeConstants.Type.CHECKBOX)
				fldObj.type = fieldTypeConstants.Type.CHECKBOX;
			fldObj = fixMissingProperties(fldObj);
			return fldObj != null ? fieldMetadata.wrap({
														   delegate: field.create(fldObj),
														   category: fieldMetadata.Category.CURRENT_SUBLIST
													   }) : null;
		}
notably, it uses nlapiGetLineItemField, which doesnt need a line
and you can avoid the validation by passing a value that is not a number
n
Oh, NetSuite itself is using nlapiGetLineItemField 😄
I will try it and see if it works.
b
spoilers, ss2 is a wrapper around ss1
💯 1
n
but with this there is always a chance it breaks when NetSuite will make any under-the-hood changes.
b
to be clear, its a lot safer using ss1 than it is relying on bugs
n
yeah, I guess so. Since NS is also using SS1 in SS2 code, we can follow the same practice. It seems to work without ANY issues and gets the job done without worrying about things breaking in future.