Adding custom functionality to standard NetSuite forms can enhance user experience and streamline data management. One way to do this is by adding a virtual field to the pricing sublist on customer records. The following guide explains how to create a user event script to add a virtual field named Custom Pricing to the Item Pricing sublist.
Overview of the Code
The provided SuiteScript 2.0 user event script creates a custom field in the pricing sublist of a customer record. This field is sourced from the Price Level list and is added when the record is in edit mode.
Step-by-Step Explanation
- Dependencies: The script requires three modules:
N/record,N/log, andN/ui/serverWidget. - Function Setup:
- The
beforeLoadfunction is triggered before the form loads. This ensures the custom field is available when the record is opened for editing.
- Context and Form Access:
- The script retrieves the form from the
contextobject. - The Item Pricing sublist is identified using its ID,
itempricing.
- Adding the Custom Field:
- The script uses
addField()to create a new field (custpage_custom_pricing) with the typeSELECT. - The field’s source is set to
pricelevel, linking it to NetSuite’s existing price level list. - The field’s display type is set to
ENTRY, making it editable in the form.
- Error Handling:
- A
try-catchblock captures and logs any errors that occur during the script execution.
Full Code for Adding the Virtual Field
define(['N/record', 'N/log', 'N/ui/serverWidget'], (record, log, serverWidget) => {
function beforeLoad(context) {
try {
// Retrieve the form from the context object
var form = context.form;
// Locate the Item Pricing sublist by its ID
var itemPricingSublist = form.getSublist({
id: 'itempricing'
});
log.debug('itemPricingSublist', itemPricingSublist);
if (itemPricingSublist) {
// Add a custom virtual field to the sublist
itemPricingSublist.addField({
id: 'custpage_custom_pricing',
type: serverWidget.FieldType.SELECT,
label: 'Custom Pricing',
source: 'pricelevel'
}).updateDisplayType({
displayType: serverWidget.FieldDisplayType.ENTRY
});
}
} catch (error) {
// Log errors for debugging
log.error({
title: 'Error in beforeLoad user event script',
details: error.message
});
}
}
return {
beforeLoad: beforeLoad
};
});
Result and Use Case
When this script is deployed to customer records in NetSuite, it adds a virtual Custom Pricing field to the Item Pricing sublist. Users can interact with this field to select different pricing levels directly within the sublist. This approach is useful for adding quick reference fields or input options that enhance data management workflows.