How To Call Functions With Form Buttons In SuiteScript

Implementing “JavaScript” in Netsuite’s SuiteScript is far from straight forward. Turns out Oracle has baked in all kinds of “hidden treasures” in their … uhhh… let’s go with creative… take on JavaScript coding standards. Their extensive, but often not-very-helpful documentation gives just enough hints to almost get things working, but then leaves out key details along the way. This has led to a half-dozen “helper articles” on this blog just in case these little tidbits are knowledge are ever needed for a future project — or for those that might stumble across these notes while searching the Internet for better Netsuite coding examples.

For the content of this post, it is assumed you have a basic working knowledge of Netsuite and SuiteScript, including a cursory knowledge of the UI module’s serverWidget and the Form implementation (as with the other Netsuite articles, you’ll need to be logged into your Oracle Netsuite account to view the SuiteAnswers links posted here).

The Task -call a function with a form button

The “ask” is simple, display a button at the top of a NetSuite form that fires a function when clicked. Simple, right? Not so fast. Calling functions with form buttons in SuiteScript is not so straightforward. If you are using the Netsuite UI module things are about to get a lot more complicated.

Adding the button to the form

Pretty straightforward, you setup your form object in SuiteScript then create the button with something like this — here the button is being added to the top of a Sublist section on the form.

...
        const geocodeOrders = () => { console.log('geocoding...'); }

        const list = nsForm.addSublist({
            id: 'weborderlist',
            label: 'Web Orders',
            type: serverWidget.SublistType.LIST
        });
        list.addButton( { id: 'geocode', label: 'Geocode', functionName: 'geocodeOrders' } );

...

This renders something like this…

A Form sublist button

This button should call a local function named “geocodeOrders” , or so you’d think if you’ve worked with any modern JavaScript framework. But no, not in SuiteScript. Things are different here.

What about referencing the running Script ID as form’s client script ID?

That won’t work because the SuiteScript processor (API) blocks the Suitelet script ID from being used as a client script. If you reference a script marked as a “Suitelet type” it will barf out one of Netsuite’s well formed messages:

{
"type":"error.SuiteScriptError",
"name":"INVALID_SCRIPT_TYPE_FOR_FORM",
"message":"This API is only supported in client entry point scripts and custom modules.",
"stack":["Error\n at Object.onRequest (/SuiteScripts/d2c_testlet/d2c_Testlet.js:86:9)"],
"cause":{
"type":"internal error",
"code":"INVALID_SCRIPT_TYPE_FOR_FORM",
"details":"This API is only supported in client entry point scripts and custom modules.",
"userEvent":null,
"stackTrace":[
"Error\n at Object.onRequest (/SuiteScripts/d2c_testlet/d2c_Testlet.js:86:9)"
],
"notifyOff":false
},
"id":"",
"notifyOff":false,
"userFacing":true
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.