N/url Module Script Samples

The following script samples demonstrate how to use the features of the N/url module:

Retrieve the Relative URL of a Record

The following sample shows how to retrieve the relative URL of a record. With the internal ID value used in this sample, the returned output is /app/accounting/transactions/salesord.nl?id=6&e=T&compid=', followed by the NetSuite account ID.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

The value used in this sample for the recordId field is a placeholder. Before using this sample, replace the recordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur.

            /**
 * @NApiVersion 2.x
 */

// This script retrieves the relative URL of a record.
require(['N/url'], function(url) {
    var output = url.resolveRecord({
        recordType: 'salesorder',
        recordId: 6,
        isEditMode: true
    });
}); 

          

Generate an Absolute URL to a Specific Resource

The following sample shows how to generate an absolute URL to a specific resource.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

The value used in this sample for the recordId field is a placeholder. Before using this sample, replace the recordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur.

            /**
 * @NApiVersion 2.x
 */

// This script generates an absolute URL to a specific resource.
require(['N/url', 'N/record'], function(url, record) {
    function resolveRecordUrl() {
        var scheme = 'https://';
        var host = url.resolveDomain({
            hostType: url.HostType.APPLICATION
        });
        var relativePath = url.resolveRecord({
            recordType: record.Type.SALES_ORDER,
            recordId: 6,
            isEditMode: true
        });
        var myURL = scheme + host + relativePath;
    }
    resolveRecordUrl();
}); 

          

Retrieve the Domain for Calling a RESTlet

The following sample shows how to retrieve the domain for calling a RESTlet.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

The value used in this sample for the accountId field is a placeholder. Before using this sample, replace the accountId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur.

            /**
 * @NApiVersion 2.x
 */

// This script retrieves the domain for calling a RESTlet.
require(['N/url'], function(url) {
    function resolveDomainUrl() {
        var sCompId = 'MSTRWLF';
        var output = url.resolveDomain({
            hostType: url.HostType.RESTLET,
            accountId: sCompId
        });
    }
    resolveDomainUrl();
}); 

          

Create a URL and Send a Secure HTTPS Post Request to the URL

The following sample shows how to get the URL to a Suitelet and send a secure HTTPS POST request to that URL with an empty body. The server’s response is also logged.

Note:

This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.

Important:

The value used in this sample for the scriptId and deploymentId fields are placeholders. Before using this sample, replace the scriptId and deploymentId values with valid values from your NetSuite account. If you run a script with an invalid value, an error may occur.

            /**
 * @NApiVersion 2.x
 */

// This script creates a URL, sends a secure HTTPS POST request to that URL, and logs the server's response.
require(['N/url', 'N/https'], function(url, https) {
    var script = 'customscript1';
    var deployment = 'customdeploy1';
    var parameters = '';
    try {
        var suiteletURL = url.resolveScript({
            scriptId: script,
            deploymentId: deployment
        });
        var response = https.post({
            url: suiteletURL,
            body: parameters
        });
        log.debug(response.body.toString());
    }
    catch(e) {
        log.error(e.toString()); 
    }
}); 

          

General Notices