Call a Suitelet from a Client Script

This sample shows how to use the https.requestSuitelet(options) method to call a Suitelet from a client-side script.

This sample consists of three scripts that perform the following actions:

Take note of the following when using the scripts in this sample:

Add a Button that Calls a Custom Function

The following user event script adds a button to a form. When the button is clicked, it calls the callSuitelet function that is defined in the referenced client script. The Form.clientScriptModulePath property is used to specify the path to the client script where the custom function is defined. Ensure that you replace the value with the path to your client script.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */

// This script adds a button to a form and calls a custom function defined in a client script.
define(['N/ui/serverWidget'], 
    
    function (serverWidget) {
        function beforeLoad(scriptContext) {
 
            // Add a button that calls the callSuitelet function
            scriptContext.form.addButton({
                id: 'custpage_call_suitelet_button',
                label: 'Click to Open Suitelet',
                functionName: 'callSuitelet'
            })
             
            // Specify the path to the client script where the callSuitelet function is defined
            scriptContext.form.clientScriptModulePath = './cs_request_suitelet.js' // Replace with the path to your client script
        }

        return {beforeLoad} 
    }); 

          

Create a Custom Function to Call a Suitelet

The following client script defines a callSuitelet function that sends a request to a Suitelet using the https.requestSuitelet(options) method. The response from the Suitelet is then shown in an alert dialog using the dialog.alert(options) method. To use this script, replace the Suitelet script and deployment IDs with your own values.

            /**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

// This script defines a custom function to call a Suitelet.
define(['N/https', 'N/ui/dialog'],
 
    function(https, dialog) {
        function pageInit(scriptContext) {}

        // Create a custom function that calls a Suitelet and displays the response in a dialog
        function callSuitelet() {
            const response = https.requestSuitelet({
                scriptId: 'customscript_sl_plf_requestsuitelet', // Replace with your Suitelet script ID
                deploymentId: 'customdeploy_sl_plf_requestsuitelet', // Replace with your Suitelet deployment ID
            });
            dialog.alert({
                title: 'Suitelet Response',
                message: response.body
            });
        }
     
        return {
            pageInit: pageInit,
            callSuitelet: callSuitelet
        };
         
    }); 

          

Create a Simple Suitelet

The following Suitelet returns a plain text response with the "Hello World!" message.

Note:

This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

            /**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */

// This Suitelet shows the text 'Hello World!' in plain text.
define([],

    function() {
        function onRequest(context) {
            context.response.setHeader({name: 'Content-Type', value: 'text/plain'});
            context.response.write('Hello World!');
        }

        return {onRequest};

    }); 

          

General Notices