1
votes

I have looked around and I cannot find an example of exactly how to call a back end Suitelet from a user event in SS 2 and return either JSON or plain text. I have a common Suitelet that I want to attach to a number of user events, I can pass the needed values simply in the url or as an object. When I try the data that is returned is an HTML page and in the middle of the page is states: "Notice You must log in before accessing this page. I am passing the compid in the url already, so I am not sure as to what to do. I have used backend Suitelets from the client side many times with no problem. Any help is appreciated.

2

2 Answers

6
votes

I would suggest using SS 2.0 custom module script library, instead of calling suitlet from UE script. Anyway, if you really want to do it with suitlet, go to your suitlet script deployment and check the "Available without login" checkbox. The code in your UE script should look like this:

// in UE script, make sure you resolve properly the suitlet external URL:
var suitletURL = url.resolveScript({
        scriptId: 'customscript_your_suitlet_scriptid',
        deploymentId: 'customdeploy_your_suitlet_deploymentid',
        returnExternalUrl: true
});

var response = https.post({
        url : suitletURL,
        headers : myHeaders,
        body : myBody
});
1
votes

Using SS2.0 modules is the most optimal solution. For SS1.0 you may consider including this code as a library and then have it shared among multiple User Event scripts.

With regards to marking your Suitelet as "Available Without Login", it is a bad security practice. You do not want your code to be available to anybody who may have a URL to call your Suitelet (it might hard to guess the exact URL, but it may be found in logs, e.g. in proxies) and secondly you may want to log the user who invoked the script for the audit purposes.

Ideally, you should be able to tell NetSuite to reuse your current user session, but unfortunately there is no API like that available as of now (it may absence for a reason).

If you cannot use SS2.0 modules or SS1.0 libraries for some reasons, a better approach would be to get a JSESSIONID cookie in your User Event script and add it to the request to your Suitelet. So the Suitelet will be then called under the context of the current user session. But be aware of potential performance implications because of a network call involved and chained user events (if your Suitelet does any record operation, it triggers user event scripts for this record).

Here is an example of SS1.0 script echoing all cookies.

/** 
* @param {nlobjRequest} nsRequest
* @param {nlobjResponse} nsResponse
*/
function kkzTestSuitelet(nsRequest, nsResponse) {
       nsResponse.writeLine('SUCCESS - Suitelet call okay, headers follow:');
       nsResponse.writeLine('');

       var allHdrs = nsRequest.getAllHeaders();
       for (var hdrName in allHdrs)
             nsResponse.writeLine(hdrName + ' = ' + JSON.stringify(allHdrs[hdrName]));
}