Search code examples
javascriptnetsuitesuitescriptsuitescript2.0

Suitescript 2.x Custom module script is not working with Render module


I'm trying to call a script with a button that I created on specific file type. It originally called a client script but I realized the render module is for server scripts only. I thought switching to a custom module would allow me to use the render module but I keep getting the same error I got with the client script. Is there another script type I should be using or is there something wrong with my code?

Error that shows up in the console

Custom module script:

/**
*name.js
*@NApiVersion 2.x
*@NModuleScope Public
*/

define(['N/record', 'N/render'], 

/**
* 
* @param {record} record 
* @param {render} render 
* @returns 
*/



  function (record, render) {

      function customButtonSendInvoice(transaction){
          console.log("The button is working.")
      }

      return{
          customButtonSendInvoice: customButtonSendInvoice
      }
  });

User Event script that creates the button:

    /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/ui/serverWidget', 'N/log', 'N/render'],
    /**
 * @param{serverWidget} serverWidget
 * @param{log} log
 * @param{render} render
 */
    (serverWidget, log, render) => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            try{
                const form = scriptContext.form;
                var transaction = scriptContext.newRecord.getValue({fieldId: 'tranid'});
                form.addButton({
                    id: 'custpage_send_invoice_button',
                    label: 'Send Invoice',
                    functionName: `customButtonSendInvoice("${transaction}")`
                }); 
                form.clientScriptModulePath ='SuiteScripts/BHM Customizations/name.js'
                
            }
            catch(e){
                log.error({
                    title: e.name,
                    details: e.message
                })
            }
        }
        
        return {beforeLoad }

    });

Solution

  • As Brett says, "as long as you're calling that from a server script" it should work. But your screenshot looks suspiciously like the Chrome devtools, which means it's running as a client script. The usual way to do this sort of thing would be to put your "working" code into a Suitelet, and call the suitelet from the client script.