Prefilling Form Fields Dynamically Using Form.updateDefaultValues

The Form.updateDefaultValues(options) method in SuiteScript 2.x is used to set or update the default values for fields in a Suitelet form. This is a convenient way to prefill form fields with specific values before they are presented to the user, enhancing usability and efficiency.

Method Syntax

form.updateDefaultValues(options);

Below is an example of how to use Form.updateDefaultValues(options) in a Suitelet:

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/ui/serverWidget'], function(serverWidget) {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            // Create a new form
            var form = serverWidget.createForm({
                title: 'Update Default Values Example'
            });


            // Add fields to the form
            var customerField = form.addField({
                id: 'custpage_customer',
                type: serverWidget.FieldType.SELECT,
                label: 'Customer',
                source: 'customer' // Source list of customer records
            });


            var dateField = form.addField({
                id: 'custpage_date',
                type: serverWidget.FieldType.DATE,
                label: 'Transaction Date'
            });


            // Set default values for the fields
            form.updateDefaultValues({
                custpage_customer: '123', // Internal ID of a customer
                custpage_date: '12/24/2024' // Default transaction date
            });


            // Display the form
            context.response.writePage(form);
        }
    }
    return {
        onRequest: onRequest
    };
});


Leave a comment

Your email address will not be published. Required fields are marked *