Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The NetSuite Pro

The NetSuite Pro Logo The NetSuite Pro Logo

The NetSuite Pro Navigation

  • Home
  • About Us
  • Tutorials
    • NetSuite Scripting
    • NetSuite Customization
    • NetSuite Integration
    • NetSuite Advanced PDF Templates
    • NetSuite Reporting & Analytics Guide
    • Real-World NetSuite Examples
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • About Us
  • Tutorials
    • NetSuite Scripting
    • NetSuite Customization
    • NetSuite Integration
    • NetSuite Advanced PDF Templates
    • NetSuite Reporting & Analytics Guide
    • Real-World NetSuite Examples
  • Blog
  • Contact Us
Home/ Real-World NetSuite Examples/Add a Secret Key Field to a Suitelet Form

Add a Secret Key Field to a Suitelet Form

πŸ” Business Use Case

Sometimes you need a way to securely capture a password or secret input in NetSuite β€” whether for:

  • Connecting to a secure external system (e.g., SFTP, API)
  • Encrypting/decrypting files
  • Storing credential tokens securely using NetSuite’s Key Management

With SuiteScript 2.0, you can now use the addSecretKeyField() method to create a secure password field that behaves differently from a regular text field:

  • βœ… Value is never visible in plain text
  • βœ… Cannot be read back via client script
  • βœ… Can be safely used in keyControl for encryption operations

πŸ’‘ Goal

Create a Suitelet Form that:

  • Prompts the user to enter a secure password
  • Accepts a PEM file from the File Cabinet (as a private key)
  • Encrypts or saves a keyControl key using that password and the PEM

πŸ’» Script: Add a Secret Key Field in a Suitelet

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget', 'N/file', 'N/keyControl', 'N/runtime'], 
function(serverWidget, file, keyControl, runtime) {
    function onRequest(context) {
        var request = context.request;
        var response = context.response;

        if (request.method === 'GET') {
            var form = serverWidget.createForm({
                title: 'Enter Secure Password'
            });

            var secretField = form.addSecretKeyField({
                id: 'custfield_password',
                label: 'Password',
                restrictToScriptIds: [runtime.getCurrentScript().id],
                restrictToCurrentUser: true  // Ensures this field can only be accessed by current user
            });
            secretField.maxLength = 64;

            form.addSubmitButton();
            response.writePage(form);
        } else {
            // POST request β€” handle submitted password token
            var passwordToken = request.parameters.custfield_password;

            // Load PEM key file from File Cabinet (ID must be known)
            var pem = file.load({ id: 422 }); // Replace with actual file ID

            // Create key using KeyControl module
            var key = keyControl.createKey();
            key.file = pem;
            key.name = 'MySecureKey';
            key.password = passwordToken;

            key.save(); // Save the encrypted key
        }
    }

    return {
        onRequest: onRequest
    };
});

πŸ” Explanation of Key Concepts

ConceptDescription
addSecretKeyField()Adds a password-style input that is encrypted internally by NetSuite. Cannot be read as plain text.
restrictToScriptIdsLimits which scripts can access this field value. Prevents misuse across scripts.
keyControl.createKey()Allows programmatic creation of secure keys that can encrypt/decrypt content or integrate with external platforms.
file.load()Used here to load a PEM file (private key) from the File Cabinet.

πŸ›‘οΈ Why Use This Instead of a Text Field?

Using a regular FieldType.PASSWORD or TEXT is not secure. They can be exposed via logging or client scripts.

The Secret Key Field encrypts the input and makes it accessible only at runtime for authorized scripts β€” making it ideal for sensitive data.


πŸ” Example Use Cases

ScenarioSecret Key Usage
πŸ” Encrypting files before uploadPassword-protect a file using keyControl
πŸ”— Connecting to SFTP using private key + passphraseUse keyControl with SFTP connection logic
πŸ”‘ Storing OAuth token secrets securelyEncrypt API tokens using secret-protected key
🧾 Encrypting custom field dataHash user input with key + password combo

πŸ“Œ Important Notes

  • The addSecretKeyField() value is one-time usable β€” it’s not stored like other field inputs.
  • You’ll need to know your File Cabinet ID for your PEM/private key.
  • Make sure to grant only authorized roles access to this Suitelet.

πŸ§ͺ Want to Expand?

You can enhance this page to:

  • Upload PEM file dynamically from the user
  • Add key expiration or management UI
  • Log successful encryption attempts to a custom record
  • Integrate with external APIs or banks using encrypted tokens

Let me know if you’d like to build that next!

Share
  • Facebook

Leave a ReplyCancel reply

Sidebar

Ask A Question

Stats

  • Questions 6
  • Answers 6
  • Best Answers 0
  • Users 3
  • Popular
  • Answers
  • Rocky

    Issue in running a client script in NetSuite SuiteScript 2.0 ...

    • 1 Answer
  • admin

    How can I send an email with an attachment in ...

    • 1 Answer
  • admin

    How do I avoid SSS_USAGE_LIMIT_EXCEEDED in a Map/Reduce script?

    • 1 Answer
  • admin
    admin added an answer The issue is usually caused by following Wrong script file… September 14, 2025 at 10:33 pm
  • admin
    admin added an answer Steps to send an Invoice PDF by email: define(['N/email', 'N/render',… August 28, 2025 at 3:05 am
  • admin
    admin added an answer This error means your script hit NetSuite’s governance usage limit… August 28, 2025 at 3:02 am

Top Members

Rocky

Rocky

  • 1 Question
  • 22 Points
Begginer
Sophie1022

Sophie1022

  • 0 Questions
  • 20 Points
Begginer
admin

admin

  • 5 Questions
  • 2 Points

Trending Tags

clientscript netsuite scripting suitescript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Menu

  • Home
  • About Us
  • Tutorials
    • NetSuite Scripting
    • NetSuite Customization
    • NetSuite Integration
    • NetSuite Advanced PDF Templates
    • NetSuite Reporting & Analytics Guide
    • Real-World NetSuite Examples
  • Blog
  • Contact Us

Quick Links

  • NetSuite Scripting
  • NetSuite Customization
  • NetSuite Advanced PDF Template
  • NetSuite Integration
  • NetSuite Reporting & Analytics

Subscribe for NetSuite Insights....

© 2025 The NetSuite Pro. All Rights Reserved