Monday, 26 May 2025

Custom List Entries Made Effortless – Just Script It with SuiteScript!

Business Scenario:

 In certain situations, business users may need the ability to manage a specific custom list field.


Example:

There is a custom list named "Color Names" with the internal ID "customlist_color". It currently contains the following values: Red, Green, Yellow, and Orange. The user would like to add a new color to this list.

Limitations:

 Custom lists in NetSuite do not support direct permission controls. Unlike standard or custom records, you cannot assign custom roles to modify customlist_* values.

Current Process:

 Currently, users must contact NetSuite admins to add new custom list values, which can cause delays due to approval steps and SOX compliance.

Alternative Solution:

We can implement this functionality through a SuiteLet script.

You can create a simple SuiteLet script that includes a single text field where users can enter a new value. Upon submitting the form, the script will automatically add the entered value to the specified custom list. 

Sample SuiteLet Code:


/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/ui/serverWidget','N/record','N/log','N/search'],
function(ui, record, log, search) {

// Replace with your custom list ID
const CUSTOM_LIST_ID = 'customlist_color_names';

function onRequest(context) {
if (context.request.method === 'GET') {
const form = homePage();
context.response.writePage(form);
} else {
const value = context.request.parameters.custpage_new_value_to_list;

try {
if (checkDuplicateValue(value)) {
buildErrorPage(context, value);
return;
}

const newId = addNewValueToCustomList(value);
buildSuccessPage(context, value, newId, context.request.url);

} catch (e) {
log.error('Error adding value to custom list', e);
context.response.write(`Error: ${e.message}`);
}
}
}

function homePage() {
const form = ui.createForm({ title:'Add New Value to Custom List'});
form.addField({
id: 'custpage_new_value_to_list',
type: ui.FieldType.TEXT,
label: 'Enter New Value for Custom List'
}).isMandatory = true;

form.addSubmitButton({ label: 'Add to Custom List' });
return form;
}

function checkDuplicateValue(value) {
const resultSet = search.create({
type: CUSTOM_LIST_ID,
filters: [['name', 'is', value]],
columns: ['internalid']
}).run().getRange({ start: 0, end: 1 });

return resultSet.length > 0;
}

function addNewValueToCustomList(value) {
const rec = record.create({ type: CUSTOM_LIST_ID });
rec.setValue({ fieldId: 'name', value: value });
return rec.save();
}

function buildErrorPage(context, value) {
const form = ui.createForm({title:'Duplicate Entry Detected'});
form.addField({
id: 'custpage_error',
type: ui.FieldType.INLINEHTML,
label: ' '
}).defaultValue = `
<div style="
padding: 10px 0;
color: #cc0000;
font-family: Arial, sans-serif;
font-size: 17px;
padding-left: 10px;
margin-top: 20px;">
<strong>Error:</strong>
The value "<strong>${value}</strong>"
already exists in the custom list.
<br><br>
<strong style="color:green">Resolution:</strong>
<span style="color:green">
Please enter a unique value.
</span>
</div>`;
context.response.writePage(form);
}

function buildSuccessPage(context, value, id, requestUrl){
const form = ui.createForm({ title: 'Success' });

form.addPageLink({
type: ui.FormPageLinkType.CROSSLINK,
title: 'Back to Form',
url: requestUrl
});

form.addField({
id: 'custpage_success',
type: ui.FieldType.INLINEHTML,
label: ' '
}).defaultValue = `
<div style="
color: green;
font-family: Arial, sans-serif;
font-size: 17px;
margin-top: 20px;">
<strong>Successfully</strong> added
"<strong style="color:#4d5f79">${value}
</strong>" to the custom list.
<br><br><strong>Internal ID:</strong>
<strong style="color:#4d5f79">${id}</strong>
</div>`;

context.response.writePage(form);
}

return {
onRequest
};
});


SuiteLet UI Sample Screen Shots:


1. Home Page:




2. Success Page:




3. Error Page


4. Ensure the new list value has been successfully added.



Other Possible Scenarios:

Similar to adding new values, existing entries in a custom list can also be deleted as needed.

Final Thoughts:

Granting unrestricted access to manage custom list values poses risks, such as accidental deletions or incorrect entries.

To mitigate this, access should be restricted to authorized users by validating roles or internal IDs (e.g., 'If user role is X' or 'If internal ID is Y'). This ensures data accuracy and system integrity.


Sample SuiteLet OutPut Video:





No comments:

Post a Comment