I’m trying to add a new field to a custom form for...
# suitescript
j
I’m trying to add a new field to a custom form for a custom record in UserEvent script, and have it appear in a particular field group that already exists on the form. I’ve tried using the
container
to specify the ID of the field group where the new field should be placed but if I include that option in the call to
addField()
I get an unexpected error (adding the field to the form works otherwise, but it doesn’t put it where I want). I’ve tried both with the
fg_
at the start and without.
Copy code
var my_new_field = context.form.addField({
	id: 'custpage_my_new_field',
	label: 'My New Field',
	container: 'fieldGroup477'				
});
watching following 1
n
I think what you may be looking for is Form.insertField() It allows you to define the position on the form that your field shows up at.
So instead of setting the container, you just find the next field in the list under that section and set it to the nextField parameter in form.insertField
j
That might work, though I want it to be the last field in the group, and insertField seems to add BEFORE the other field.
m
@jen My go-to for that scenario is to insert the new field before the last field, and then insert the last field before the new field. It's a bit clunkier than I'd like but it's reliable at least. I do wish there was something like an
afterField
option in
insertField()
in addition to
nextfield
though (not to mention the terribly inconsistent capitalization with what should be
nextField
).
💯 1
j
oooh smart
so you can insertField() an existing field to move it AFTER a field you’ve added in the UE
m
@jen Yeah, exactly - just use
getField()
on the existing field since
insertField()
only takes a field object (not an ID) for the field you're moving.
j
no joy unfortunately. Looks like I can’t use insertField() to move an existing field (one not added by UE).
d
just tested and got the same results (unexpected error). Also tried to
serverWidget.createAssistant({title:'test'}).getFieldGroupIds()
to get the field group ids of the non-scripted field groups. But this resulted in an "insufficient permissions" error (on a UE script running as admin!?) Also tried
fg_fieldGroup##
and
fieldGroup##
as the container ids. However I can confirm using the tab id as the container does work
my only two solutions would be: • add a hidden dummy field to the end of the field group and
insertField()
before that • DOM hack your scripted field into position (ugh... I know, I know.. smh..)
m
That's odd, I definitely have examples of doing this. Here's the general pattern I use:
Copy code
var scriptedField = context.form.addField({
	'id':'custpage_scripted_field',
	'type':serverWidget.FieldType.TEXT,
	'label':'Scripted Field',
	'container':'items'
});
var existingField = context.form.getField({'id':'custbody_existing_field'});
context.form.insertField({
	'field':scriptedField,
	'nextfield':'custbody_existing_field'
});
context.form.insertField({
	'field':existingField,
	'nextfield':'custpage_scripted_field'
});
I haven't tested if it'll work without specifying the
container
if it's on a tab, so maybe that's part of the issue? If it's just a field group though, I definitely have functional examples without a
container
specified on the
addField()
.
d
hmmm... that's interesting Nathan So you're able to add a scripted field to the end of an existing field group? And to do so you are not specifying the
container
parameter, just doing the double
insertField()
trick?
I see in your example that you're moving an existing custom field with
insertField()
. I wonder if the difference with Jen's testing was that they were trying to move an existing standard/native field.
j
one field is existing custom, one is field added by UE
latter needs to be last
👍 1
d
actually yeah, just tested the double
insertField()
trick à la Nathan's example. I was able to get it to work with not only an existing custom field, but also an existing standard/native field
j
weird. I will try again!
🤞 1
Still not working. My new (UE-created) field still ends up showing BEFORE my existing (exists on Custom Record) custom field. Both are in the right Field Group but just in the wrong order.
Copy code
var ue_created_field = context.form.addField({
	id: 'custpage_ue_created_field',
	type: serverWidget.FieldType.LONGTEXT,
	label: 'UE Created Field'
});

context.form.insertField({field: ue_created_field, nextfield: 'custrecord_existing_field_in_field_group'});

// Get existing field.
var existing_field_in_field_group = context.form.getField({fieldId: 'custrecord_existing_field_in_field_group'});

context.form.insertField({field: existing_field_in_field_group, nextfield: 'custpage_ue_created_field'});
m
Try logging out
existing_field_in_field_group
-
Form.getField()
uses
id
as the option, not
fieldId
, so I'm guessing your field variable is null. Note that, in classic NetSuite fashion,
CurrentRecord.getField()
uses
fieldId
- that inconsistency (along with the all-lower-case
nextfield
) has always bothered me about this whole workaround, and both have definitely tripped me up in the past.
j
ohhhhh oops
FFS NS be consistent
damn my try catch
IT
WORKED
OMG @MTNathan @David B @Nathan L you are lifesavers.
netsuite 1
👍 3
n
Lol this was a whole journey
d
I'm just here for the ride 😆 but glad I gained a new trick along the way