I want to add a field to a form on beforeLoad in t...
# suitescript
j
I want to add a field to a form on beforeLoad in the UI. I’d like to add it to a certain field group. How can I determine the ID of the “container” (the field group)? The group is not added via SuiteScript, it’s already in the form.
m
I believe I've accomplished this by using
form.insertField()
- the field you're inserting essentially inherits the field group of the location you're inserting it, if that makes sense. You may also be able to get the field group id by inspecting the page's html but personally I have only done that for tabs, not field groups.
e
Inspect the Form's HTML; there should be a
<td>
containing the Field Group. The
id
attribute will have the Field Group's script ID, prefixed with
fg_
, e.g.
fg_custpage_grp_prompt
message has been deleted
j
So I tried
form.insertField()
but it refuses to put it before
nextfield
and it just puts it at the top.
which is why I thought to try the group as container
but when I put the group id in I just get an unexpected error
fun times
m
Is your
nextfield
set up with 'Same Row as Previous' on your custom form setup? I think I've run into issues with that in the past.
Side note (I think you've complained about NetSuite's inconsistencies before as well): why is
nextfield
not camel-case like every other option name?
j
right?
it’s not set as same row as previous
message has been deleted
message has been deleted
Copy code
var button_field = context.form.addField({
			id: 'custpage_buttons',
			label: 'custpage_buttons',
			type: serverWidget.FieldType.INLINEHTML
		}).updateLayoutType({
			layoutType: serverWidget.FieldLayoutType.OUTSIDE
		}).updateBreakType({
			breakType: serverWidget.FieldBreakType.STARTROW
		}).defaultValue = button_html;			
			
		context.form.insertField(button_field, 'custrecord_results');
m
Ah, I think it's that OUTSIDE layoutType that's the issue, try removing that
j
ohh
hahhah probably, that’s just my default to get things to $*&#ing go where I want
that was it! of course it’s actually going right AFTER the field I want it before now, but that’s better than being at the top
m
Oh yeah, I've done plenty of that sort of thing.
j
you are a genius
m
Ha, I wouldn't say that but I'm glad I could help
j
weird that it’s appending rather than going before but, meh, I can deal
m
That is odd, maybe the
breakType
is causing that?
j
I tried taking out the breaktype but it didn’t make a difference
message has been deleted
Copy code
var button_field = context.form.addField({
			id: 'custpage_buttons',
			label: 'custpage_buttons',
			type: serverWidget.FieldType.INLINEHTML
		}).defaultValue = button_html;			
			
		context.form.insertField(button_field, 'custrecord_results');
m
Assuming
custrecord_results
is the red text in your screenshot, that looks right to me. Maybe try using an object for the options instead of the ordered parameters?
j
did that, non change. It’s not right, it’s supposed to be before the red field
nextfield = The internal ID name of the field you are inserting a field in front of.
m
Yeah, that's how it's always worked for me in the past. The only other thing you might try is to intentionally insert
custrecord_results
in front of your scripted field (using
form.getField()
) and then insert your scripted field in front of
custrecord_results
. It feels really clunky and there's probably a better way, but I've done that in the past to get a field to show up as the first/last in a field group when it was otherwise giving me trouble.
And as they say, if it's stupid and it works, it's not stupid.
j
Just for context, this is why it matters (closer to what it will actually look like, note that I will actually be hiding the group labels, they are just there to aid with layout)
message has been deleted
obvs want my button above the results that will be displayed
m
Worst case scenario, you could hide your non-scripted field (which I assume you want to store data) and script another one that just mirrors it so you can have more direct control over the layout
But that also feels unnecessary when
insertField
should just work the way it says it does...
j
yeah
Ugh. Of course form.getField doesn’t have a fieldId option, just id
I hate everything
m
Oh yeah, but it's
fieldId
if you're doing it client-side using
context.currentRecord
, of course.
j
of course
anyhow, thanks for all your help. I’ll move on with this for now and ask NS about the field ordering, not a biggie for the time being. Just a “for fun” tool I’m building anyhow.
m
No problem, glad I could at least get you part-way there!