ANS-1872 · SUITESCRIPT DEVELOPMENT
How to Inject Custom CSS into a NetSuite Record Form Header
Leverage a user event script and an inline HTML field to dynamically add a custom stylesheet to NetSuite record forms.
Short answer
To add a custom CSS file to a NetSuite record form's header, host the CSS file in the File Cabinet, ensuring public accessibility. Then, create a user event script with a `beforeLoad` function that adds an inline HTML field. This field's default value will contain JavaScript that creates and inserts a stylesheet link element into the form's header.
Scenario
NetSuite users often require custom styling for record forms to enhance user experience or align with specific branding guidelines. The standard NetSuite interface does not provide a direct method for adding custom CSS files to the header of individual record forms. This scenario addresses the need to dynamically load a custom stylesheet when a record form is accessed.
Solution
Host the Custom CSS File: Upload your custom CSS file and any associated images to the NetSuite File Cabinet. Ensure that the file's availability is set to "Available Without Login" to allow public access.
Create a User Event Script: Develop a new user event script in SuiteScript 2.x. This script will execute during the
beforeLoadevent of the target record form.
javascript
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define([], function() {
function beforeLoadTestInject(context) {
var form = context.form;
var cssScriptField = form.addField({
id: 'custpage_cssfield',
type: 'inlinehtml',
label: 'Custom CSS Injector'
});
cssScriptField.defaultValue = "<script type="text/javascript">var linkElement = document.createElement('link');linkElement.rel = 'stylesheet';linkElement.href = 'https://system.sandbox.netsuite.com/core/media/media.nl?id=577&c=05801&h=F4390276d84372784aa&_xt.css';document.getElementsByTagName('head')[0].insertBefore(linkElement,document.getElementsByTagName('head')[0].firstChild);</script>";
}
return {
beforeLoad: beforeLoadTestInject
};
});The script above creates an inline HTML field (custpage_cssfield) on the record form. The defaultValue of this field contains a JavaScript snippet. This JavaScript dynamically creates a ` element for a stylesheet and inserts it as the first child within the ` section of the HTML document. This ensures the custom CSS loads early in the page rendering process. It is important to note that this method involves direct Document Object Model (DOM) manipulation, which NetSuite documentation explicitly advises against, recommending the use of SuiteScript APIs for UI interactions. Additionally, NetSuite has indicated that inline HTML fields on standard record forms, outside of Suitelets, are subject to deprecation in a future release. Therefore, this approach should be considered a short-term solution and used with caution, as it may lead to unexpected behavior or require adjustments in future NetSuite versions.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
