Search code examples
xmlhttprequestnetsuitesuitescript2.0

NetSuite SuiteScript how to access clientscript function from custom button


I have a suitelet that shows a list of records. At the end of each record I want a button that calls a function in the client script. If I put the function outside of the define() function, it works. My issue is I need the function inside the define() one so I can access NetSuite modules like N/https.

Suitelet button being added:

inAreaSublist.setSublistValue({id: 'newcol', line: i, value: '<button onclick="testFunction(' + customerId +',' + mResult.custid +')">Mark Connected</button>'});

Clientscript

function testFunction(cust1, cust2){
    console.log(cust1 + ' ' + cust2);
    https.post({
        url: 'this will be a resetlet'
    });
};

So if testFunction() is outside of define() it logs correctly but says https is not defined. If I do it inside define() it says testFunction is not a function.

Any thoughts? If i replace the button with a hyperlink pointing to a suitelet everything works, but it is ugly popping up a window briefly to execute the suitelet code. So I thought an XHR request would be best.

Thanks!


Solution

  • Not sure if this is an optimal solution, but I'd try this (with your testFunction declared outside of the define() call):

    'use strict';
    
    function testFunction(cust1, cust2){
      require(['N/https'], function(https) {
        https.post({
            url: 'this will be a resetlet'
        });
        console.log(cust1 + ' ' + cust2);
      });
    };
    

    So you need just to make a require call inside you testFunction and require all the modules you need. They should be available (provided that they are documented as available in client scripts)