Search code examples
netsuitesuitescript

Why does my suitelet return a login page as response?


I created a suitelet that returns a response to my client script. Despite of what I put into my suitelet response, when I try to read my response.body property in my client script it contains html that renders a netsuite login page.

Call from client script:

var domain = url.resolveDomain({
hostType: url.HostType.APPLICATION
});
var scriptURL = url.resolveScript({
scriptId: 'scriptId',
deploymentId: 'deploymentId',
returnExternalURL: true
});
var suiteletUrl = "https://" +domain+scriptURL;
var results=https.get({
url: suiteletUrl
})
log.debug("Result is",results);
log.debug("Responsebody is",results.getBody);

The response.body property contains the following html code:

<!doctype html>\n\n<html>\n<head>\n\t<title>NetSuite Login</title>\n\n\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>\n\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n> ....(contd)

My suitelet code:

const onRequest = (scriptContext) => {
var html = '<html>' +
'<body>something:<br/>'
+'<form method="post">' +
' Input ' +
'<input type="text" name="something" id="something" value=""/> ' +
'<input type="submit"/> ' +
'</form></body></html>';
scriptContext.response.write(html);
}

Any idea on what is the issue?


Solution

  • If you are calling the suitelet from a client script then you want to set returnExternalURL: false if your client script is actually being called from the UI (window.location.href = resolvedURL) but true if you are calling like you did.

    You also don't need to calculate or add the domain to the URL

    If you are generating the URL to use from an external application you would need to flag the suitelet deployment as available without login and make the audience all roles.

    This is from a production client script that I wrote where the client script is being called in a Sitebuilder site with "Scriptable Checkout" turned on:

    var scriptURL = url.resolveScript({
        scriptId: 'customscript_kotn_scipt1', 
        deploymentId: 'customdeploy_kotn_script1', 
        returnExternalUrl: true
    });
    
    var resp = http.get({
        url:scriptURL
    });