N/http Module Script Samples

Explore N/http module script samples, including HTTP GET requests and redirecting records in SuiteScript 2.1.

·2 min read·View Oracle Docs

The N/http module in SuiteScript provides developers with the ability to handle HTTP requests effectively. This article illustrates two practical examples of using the N/http module, including how to make GET requests and redirect records within the NetSuite environment.

How to Request a URL Using HTTP GET

In SuiteScript, you can perform an HTTP GET request to retrieve data from a specified URL. The following sample demonstrates how to execute a GET request using the N/http module:

Code Sample: HTTP GET Request

suitescript
1/**
2 * @NApiVersion 2.1
3 */
4// This script uses an HTTP GET request for a URL.
5require(['N/http'], (http) => {
6 function sendGetRequest() {
7 let response = http.get({
8 url: 'http://www.google.com'
9 });
10 // Process the response
11 log.debug({ title: 'Response', details: response.body });
12 }
13 sendGetRequest();
14});

Important Notes:

  • Make sure to replace the URL with the desired endpoint you wish to request data from.
  • The sample script is designed for use in the SuiteScript Debugger. For production deployment, adapt it appropriately based on the script type requirements.

How to Redirect a Record

Another useful feature of the N/http module is the ability to redirect users to a specific record within NetSuite. This example shows how to create a Suitelet that redirects to a new sales order record while setting an entity field.

Code Sample: Redirecting to a Sales Order Record

suitescript
1/**
2 * @NApiVersion 2.1
3 * @NScriptType Suitelet
4 */
5// This script redirects to a new sales order record and sets the entity.
6define(['N/record', 'N/http'], (record, http) => {
7 function onRequest(context) {
8 context.response.sendRedirect({
9 type: http.RedirectType.RECORD,
10 identifier: record.Type.SALES_ORDER,
11 parameters: ({
12 entity: 6 // Ensure this is a valid entity ID
13 })
14 });
15 }
16 return {
17 onRequest: onRequest
18 };
19});

Important Notes:

  • Ensure the entity field is populated with a valid internal ID from your NetSuite account to avoid errors during execution.
  • This sample script must be used in a deployed Suitelet environment.

Key Takeaways

  • The N/http module facilitates HTTP communication within SuiteScript.
  • Use the provided samples to handle GET requests and redirects effectively.
  • Always validate IDs when passing parameters to avoid runtime errors.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

Do I need special permissions to perform HTTP GET requests using the N/http module in SuiteScript?
The article does not specify permissions, but executing scripts in NetSuite generally requires appropriate script deployment permissions. Ensure you have access rights to use SuiteScript for API interactions.
Can I use the HTTP GET request code sample in a production environment without modification?
No, the sample is designed for the SuiteScript Debugger. You need to adapt it based on production script type requirements.
What is a crucial step when redirecting to a sales order record using the Suitelet sample?
Ensure the `entity` field is populated with a valid internal ID from your NetSuite account to prevent errors during execution.
Does the article mention a feature flag that must be enabled to use the N/http module?
The article does not mention a feature flag requirement for using the N/http module. However, you should check your account's configuration to confirm module availability.
Source: N/http Module Script Samples Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →