Dynamically Add Custom Button Using User Event Scripts

Written by
Manuelito Macalinao
Published on
August 31, 2023 at 9:14:10 AM PDT August 31, 2023 at 9:14:10 AM PDTst, August 31, 2023 at 9:14:10 AM PDT

Scenario

A user wants to dynamically add a button in a record using aUser Eventscript.

Solution

  1. DeployUser Eventscriptthat will display the button

    1. CreateJavaScriptfile with the following code:

      /** * @NApiVersion 2.x * @NScriptType UserEventScript * @NModuleScope SameAccount */define([],function(){functionbeforeLoad_addButton(scriptContext){var form = scriptContext.form; form.addButton({ id :'custpage_buttonid', label :'Test', functionName :'clientButton()'}); form.clientScriptFileId =7018;//internal id of the script file in the file cabinet}return{ beforeLoad: beforeLoad_addButton }});
    2. Navigate toCustomization>Scripting>Scripts>New

    3. Click+

    4. Select File:ClickBrowse...

    5. SelectJavaScript file
      Note:Select JavaScript file created from Step 1.1.

    6. ClickOpen

    7. ClickCreate Script Record

    8. Basic Information:

      • Name:EnterAdd Button
      • ID:Enter_add_button
    9. Deployments tab:

      • Applies To:SelectEmployee
      • ID:customdeploy_add_button
      • Status:SelectReleased
    10. ClickSave

  2. UploadClient scriptthat serves as the custom button's logic

    1. CreateJavaScriptfile with the following code:

      /** * @NApiVersion 2.x * @NScriptType ClientScript * @NModuleScope SameAccount */define([],function(){functionpageInit(context){}functionclientButton(){alert('Called by a Client Script!');}return{ pageInit: pageInit, clientButton: clientButton };});
    2. Navigate toDocuments>Files>SuiteScripts

    3. ClickAdd File

    4. SelectJavaScript file
      Note:Select JavaScript file created from Step 2.1.

    5. ClickOpen