Add a Secret Key Field
The following sample shows how to add a secret key field.
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.
/**
* @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 Password'
});
var credField = form.addSecretKeyField({
id: 'custfield_password',
label: 'Password',
restrictToScriptIds: [runtime.getCurrentScript().id],
restrictToCurrentUser: true //Depends on use case
});
credField.maxLength = 64;
form.addSubmitButton();
response.writePage(form);
} else {
// Read the request parameter matching the field ID we specified in the form
var passwordToken = request.parameters.custfield_password;
var pem = file.load({
id: 422
});
var key = keyControl.createKey();
key.file = pem;
key.name = 'Test';
key.password = passwordToken;
key.save();
}
}
return {
onRequest: onRequest
};
});