Executing a RESTlet on Click via User Event Script Button

To call a RESTlet and execute it as an Administrator from another script, the call should come from a server-side script so the user credentials which includes the Role can be specified.

When calling a RESTlet from a custom button created from a User Event script, a series of scripts need to be triggered in order for it to work. Basically, the concept is this: Create the button from a User Event script that will source its on-click function from a Client script. The Client script will call a Suitelet that will, then, call the RESTlet. Even if a User Event script is technically server-side, the functionality of the custom button comes from a client-side script, that is why we need an extra Suitelet so the call to the RESTlet will come from a server-side script.

  • N/url
  • N/https
  • N/http

Suitelet

function onRequest(context) {
    log.debug(context)
    if (context.request.method == 'GET') {
        log.debug('Got Here')
        var url = 'https://<accountid>.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=203&deploy=1';
        var headers = {
            "Content-type": "application/json",
            "Authorization": "NLAuth nlauth_email=test@netsuite.com," + "nlauth_signature=sampleP@ssW0rd!," + "nlauth_account=12345," + "nlauth_role = 3"
        };
        var response = https.request({method: http.Method.GET, url: url, headers: headers});
        log.debug(response.body);
    }
}
User Event script
function beforeLoad(scriptContext) {
    var resURL = url.resolveScript({scriptId: 'customscript205', deploymentId: 'customdeploy1'});
    log.debug(resURL)
    var form = scriptContext.form;
    form.addButton({id: 'custpage_button', label: 'Run RESTlet', functionName: "goToSuitelet('" + resURL + "')" // Passing URL Parameter to the Client Script 
 }); form.clientScriptFileId = 'id'; //Internal Id of the Script file in File Cabinet 
}
Client Script

function goToSuitelet(url) {
console.log(url)
var response = https.get({ url: url });
}

Leave a comment

Your email address will not be published. Required fields are marked *