N/sso Module Script Samples

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

Generate a New OAuth Token

The following sample shows how to generate a new OAuth token for a user. This sample requires the SuiteSignOn feature.

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 suiteSignOnRecordId field is a placeholder. Before using this sample, replace the suiteSignOnRecordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur. Additionally, the SuiteSignOn record you reference must be associated with a specific script. You make this association in the SuiteSignOn record’s Connection Points sublist. For help with SuiteSignOn records, see Creating SuiteSignOn Records.

            /**
 * @NApiVersion 2.x
 */

// This script generates a new OAuth oken for a user.
require(['N/sso'], function(sso) {
    function generateSSOToken() {
        var suiteSignOnRecordId = 1;
        var url = sso.generateSuiteSignOnToken(suiteSignOnRecordId);
    }
    generateSSOToken();
}); 

          

Generate a suiteSignOn Token in a Portlet

The following sample shows how to use generateSuiteSignOnToken(options) in a portlet 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:

The value used in this sample for the suiteSignOnRecordId field is a placeholder. Before using this sample, replace the suiteSignOnRecordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur. Additionally, the SuiteSignOn record you reference must be associated with a specific script. You make this association in the SuiteSignOn record’s Connection Points sublist. For help with SuiteSignOn records, see Creating SuiteSignOn Records.

            /**
 * @NApiVersion 2.x
 * @NScriptType Portlet
 * @NScriptPortletType form
 */

// This script uses generateSuiteSignOnToken in a portlet.
define(['N/sso'], function (sso) {
    function render(context) {
        var suiteSignOnRecordId = 'customsso_test';
        var url = sso.generateSuiteSignOnToken(suiteSignOnRecordId);
    }
    return {
        render: render
    };
}); 

          

Generate a suiteSignOn Token Using a Suitelet

The following sample shows how to use generateSuiteSignOnToken(options) in a Suitelet 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:

The value used in this sample for the suiteSignOnRecordId field is a placeholder. Before using this sample, replace the suiteSignOnRecordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur. Additionally, the SuiteSignOn record you reference must be associated with a specific script. You make this association in the SuiteSignOn record’s Connection Points sublist. For help with SuiteSignOn records, see Creating SuiteSignOn Records.

            /**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

// This script uses generateSuiteSignOnToken in a Suitelet.
define(['N/sso'], function(sso) {
    function onRequest(context) {
        var suiteSignOnRecordId = 'customsso_test'; //Replace placeholder values
        var url = sso.generateSuiteSignOnToken(suiteSignOnRecordId);
    }
    return {
        onRequest: onRequest
    };
}); 

          

Generate a suiteSignOn Token Using a User Event Script

The following sample shows how to use generateSuiteSignOnToken(options) in a user event 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:

The value used in this sample for the suiteSignOnRecordId field is a placeholder. Before using this sample, replace the suiteSignOnRecordId field value with a valid value from your NetSuite account. If you run a script with an invalid value, an error may occur. Additionally, the SuiteSignOn record you reference must be associated with a specific script. You make this association in the SuiteSignOn record’s Connection Points sublist. For help with SuiteSignOn records, see Creating SuiteSignOn Records.

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

// This script uses generateSuiteSignOnToken in a user event script.
define(['N/sso'], function(sso) {
    function beforeLoad(context) {
        var suiteSignOnRecordId = 'customsso_test';
        var url = sso.generateSuiteSignOnToken(suiteSignOnRecordId);
    }
    return {
        beforeLoad: beforeLoad
    };
}); 

          

General Notices