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:
-
Add a Button that Calls a Custom Function – A user event script that adds a custom button on a form. When the button is clicked, it calls a custom function that is defined in a client script.
-
Create a Custom Function to Call a Suitelet – A client script that defines a custom function that calls a Suitelet and displays the Suitelet response in a dialog.
-
Create a Simple Suitelet– A simple Suitelet that shows the text "Hello World!".
Take note of the following when using the scripts in this sample:
-
Ensure that you replace path to the client script referenced in the user event script.
-
Ensure that you replace the script and deployment IDs of the Suitelet referenced in the client script.
-
The client script and user event script must be deployed to the same form.
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.
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.
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.
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.
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};
});