netsuitesuitescript2.0server-side-scripting

Function Not Found Error NetSuite Suitelet


I am attempting to add a button to a page in order to run a Scheduled Script on Netsuite. I have done some successful debugging up until this point. After publishing the script, I tested the form on Firefox (main browser, everyone at work uses it) and have found that the function I use to call my script is not defined. I have it defined outside of the onRequest function in order for it to be picked up by the webpage scope. My code:

    *@NApiVersion 2.x
    *@NScriptType Suitelet
    */
    define(["N/task", "N/ui/serverWidget"], function(task, serverWidget) {  
     function onRequest(context) {
      if(context.request.method === 'GET'){
       var form = serverWidget.createForm({
        title: 'Discrepancy Emailer Button'
     });
     form.addButton({
       id: 'custombutton',
       label: 'Send Emails',
       functionName: 'executeScheduled'
     });
     context.response.writePage(form);
     }
    }

    function executeScheduled() {
     var scriptTask = task.create({
      taskType: task.TaskType.SCHEDULED_SCRIPT,
      scriptId: "customscriptXXXX",
      deploymentId: "customdeploy1"
    });

    var scriptTaskId = scriptTask.submit();

    log.debug("scriptTaskId", scriptTaskId);
  }

  return {
    onRequest: onRequest
  };`
```
The error I receive:
[Uncaught ReferenceError: executeScheduled is not defined
    onclick scriptlet.nl:1
    execCb NsRequire.js:2047
    check NsRequire.js:1193
    enable NsRequire.js:1475
    init NsRequire.js:1106
    h NsRequire.js:1771
    setTimeout handler*w.nextTick/< NsRequire.js:2216
    nextTick NsRequire.js:2218
    h NsRequire.js:1760
    requirejs NsRequire.js:2167
    require bootstrap.js:155
    onclick scriptlet.nl:1
scriptlet.nl:1:25
](https://i.sstatic.net/CYDH8.png)
I would first like to understand why the error occurs as well as what I would have to change to fix it. Thank you all very much for your time. 


I have tried moving the 'executeScheduled' function before and after the button declaration, changing the type of button to a submit button, using a client side script, adding it as part of the post request and declaring the task separately. I was expecting the scheduled script to run.

Solution

  • Suitelets are executed on the NetSuite server - the code in them is not available to the browser. The way to handle your use case would be to attach a client script to the form you're returning and put the function to be executed from the button into that client script.

    form.clientScriptModulePath = 'SuiteScripts/path/to/client_script'; //file cabinet path to client script file
    

    or

    form.clientScriptFileId = 12345; //internal id of client script file in your file cabinet