Effective SuiteScript

24 How do I add a Button to a Record?


Published by November 8, 2018 3:35 pm

Level: Beginner

Category: Records

, , , ,

Prefer a video? Watch here (12 minutes)


This is definitely a question I see A LOT! How do we add a button to a record form? We actually have a few options as SuiteScript developers, so let’s see how this works.

Add Button via 2.0 Client Script (Edit only)

SuiteScript 2.0 added a very simple way to add new buttons to forms, but that simplicity carries some limitations as well.

If we take a brief step back, we know that if we’re making a button, that means we are going to have a user who is working in the user interface. The page is going to load, our button is going to display, and at some point after that, the user will click the button. When they do, we need some code loaded on the page ready to respond to that click.

The only Script type we have for responding to user interactions in the UI like that is a Client Script, so we start by creating one of those.

That’s all we’re going to do with the code right now; create and deploy this Client Script to the Employee record. Once that’s done, you should see a Buttons sublist on the Scripts subtab of your Script record. This is the first method of adding Buttons to our records.

Using this sublist, add a row for your button, giving it a Label and the name of a function to execute when the button is clicked. I’ve gone with onButtonClick. I always prefix my custom event handlers with on. That’s a naming scheme I picked up a long time ago, but it’s not mandatory so name the function whatever you’d like.

Back in our Client script code, we need to actually build that function now. What is going to happen when the user clicks our button? For this example, we’ll stick to something simple and just pop up a message for them.

We start by adding our function with the same name as we specified in the Script record.

Now because our function lives inside a NetSuite module, just like any other entry point, it can leverage other NetSuite modules. For displaying our message, let’s leverage NetSuite’s dialog module.

Make sure you add the click handler function to the output of our module as we did with exports.onButtonClick = onButtonClick; 

If you do not do this, your click handler function will not be available for the browser to call, and you will see an error in the developer console like custommodule.onButtonClick is not defined when you click the button.

Upload your changes and open an Employee record in the UI.

The first thing you should notice is that our button does not display in View mode. This is the major limitation of adding a button with this method. Because the button is being added by a Client Script, and because NetSuite only loads Client Scripts when a record is being edited, our button does not show up in View mode.

We’ll learn how to get around that shortly. For now just Edit the Employee. You should see and be able to click your button.

There’s our message. We now have a working custom button! If you only ever need a button to show up in Edit mode, then you’re all done!

Add Button via User Event

The second method for adding a button is far more flexible but does add a little more code.

Adding a button is effectively a modification to the Form object in the user interface. We want to make that modification before the form loads, before the user sees it. There are two possible entry points we know about that run before the page loads:

  1. Client Script pageInit
  2. User Event beforeLoad

As we just stated, however, Client Scripts do not run in View mode, so automatically pageInit is out. We must make a new User Event with abeforeLoad entry point.

Once again, use this source file to create a new User Event script and deploy it to the Employee record.

Note in the comments for beforeLoad that we get passed a reference to the UI Form Object via the context argument; you can also find this information in the Help page for beforeLoad.

We can leverage the Form Object’s addButton method to create our button. The properties will look the same as when we specified it in the UI.

It’s a best practice to prefix the IDs of our custom UI components with custpage_, so we’ll do that here.

We still need a way to get our Client Script to load on the Form in View mode. To do so, we can leverage the Form Object’s clientScriptModulePath property, setting it to the fully qualified path of our Client Script source file.

This forces our Client Script to be loaded whenever this User Event runs, which will include View mode. You may of course have a different path and file name, depending on where you uploaded your source file in the File Cabinet.

If you forget this step, or you have the wrong path, you will see the very same custommodule.onButtonClick is not defined Error in the browser console.

Upload your script changes, open up an Employee record in View mode, and you should see that everything is now working.

With the User Event, you have more flexible control over which Event Types will display your Button. You can either use the context.type property of beforeLoad to conditionally add the button in certain contexts, or use the Event Type field on the User Event’s Deployment record.

Conclusion

And there we go! We’ve now seen two different ways to successfully add a custom button to a record. The first way through a Client script is fast but limited. The User Event option gives us more control but adds more code. It’s up to you which method best suits your situation.


Join Effective SuiteScript for weekly content to help you learn NetSuite development