Generate Credential Field

The following sample shows how to use a Suitelet to create a form field that generates a GUID. For more information about credential fields, see Form.addCredentialField(options).

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.

Note:

The default maximum length for a credential field is 32 characters. If needed, use the Field.maxLength property to change this value.

The values for restrictToDomains, restrictToScriptIds, and baseUrl in this sample are placeholders. You must replace them with valid values from your NetSuite account.

Important:

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

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

// This script creates a form with a credential field.
define(['N/ui/serverWidget', 'N/https', 'N/url'], (serverWidget, https, url) => {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            const form = serverWidget.createForm({
             title: 'Password Form'
            });

            const credField = form.addCredentialField({
                id: 'password',
                label: 'Password',
                restrictToDomains: ['<accountID>.app.netsuite.com'],
                restrictToCurrentUser: false,
                restrictToScriptIds: 'customscript_my_script'
            });

            credField.maxLength = 32;

            form.addSubmitButton();

            context.response.writePage({
                pageObject: form
                });
        } 
        else {
            // Request to an existing Suitelet with credentials
            let passwordGuid = context.request.parameters.password;

            // Replace SCRIPTID and DEPLOYMENTID with the internal ID of the suitelet script and deployment in your account 
            let baseUrl = url.resolveScript({
                scriptId: SCRIPTID,
                deploymentId: DEPLOYMENTID,
                returnExternalURL: true
            });

            let authUrl = baseUrl + '&pwd={' + passwordGuid + '}';

            let secureStringUrl = https.createSecureString({
                input: authUrl
            });

            let headers = ({
               'pwd': passwordGuid
            });

            let response = https.post({
                credentials: [passwordGuid],
                url: secureStringUrl,
                body: {authorization:' '+ passwordGuid + '', data:'anything can be here'},
                headers: headers
            });
        }
    }
    return {
        onRequest: onRequest
    };
}); 

        

Related Topics

SuiteScript 2.x Suitelet Script Type
SuiteScript 2.x Suitelet Script Type Code Samples
Custom Forms
N/ui/serverWidget Module
N/https Module
N/url Module

General Notices