SuiteScript – Form Buttons

Buttons that perform actions can be useful when providing additional features to users throughout Netsuite.

In the past, the best method of adding a button and an action has been to use a User Event script as well as Client script to provide button and function

When developing solutions I prefer to keep the code base small so I have moved to putting the function inside the User Event script, which does away with the need for an additional client script.

Note: the below code requires Suitescript 2.1 – due to the grave symbols, it will not run in SS2

var funcDoSomething = `
require(['N/https'], function dothis(https){
	var objResponse = https.post({                    
					url: "https://netsuite.com/somepage",
					body: {
						intRec: ` + intRecID + `,
					}
				});
				window.location.assign('https://google.com');
});`

scriptContext.form.addButton({
	id: 'custpage_dosomething',
	label: 'DoSomething',
	functionName: funcDoSomething
});

The above code first defines a function, containing a whole block of Suitescript 2.0 (inclusive of the require statement) which is then called by the button further down added to the form.
The code allows for a https.post action to happen, the page is then changed to be google.com via a location.assign

The function is wrapped inside the grave (`) symbols in order to allow a multiple line block. Without these, the code would fail on the client side as it is rendered as a single line of code.

The code also references the https module – this module can be changed, or have more modules added before or after it the same as any other Suitescript 2 script file.

Any posting elements (the above uses a variable containing the record ID) can be added from the parent UE script by breaking the script apart using the grave symbol and adding the variable in.
On runtime, the function will be built with the record ID inside the function.