Retrieving all possible values for a field via a RESTlet

Learn retrieving all possible values for a field via a restlet with practical examples, diagrams, and best practices. Covers netsuite development techniques with visual explanations.

Retrieving All Possible Values for a Field via a RESTlet

Retrieving All Possible Values for a Field via a RESTlet

Learn how to programmatically fetch all available options for a NetSuite field using a custom RESTlet, enhancing integration and automation capabilities.

NetSuite fields, especially custom ones, often come with predefined lists of values. When integrating NetSuite with external systems or building dynamic user interfaces, it's crucial to programmatically retrieve these possible values. While SuiteScript offers direct access, exposing this functionality via a RESTlet provides a clean and secure API endpoint for external applications. This article guides you through creating a RESTlet to achieve this, focusing on both standard and custom fields.

Understanding Field Value Retrieval in NetSuite

NetSuite provides various ways to define field values. For standard fields like 'status' or 'type', values are often managed internally. Custom fields can be of several types, including 'List/Record' (select from a list), 'Multiple Select', or 'Free-Form Text'. Our focus will be on fields that have a discrete set of possible values. The key to retrieving these values lies in NetSuite's N/record and N/ui/serverWidget modules, specifically when dealing with records or forms.

/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 */

define(['N/log', 'N/record', 'N/search'],
    /**
     * @param {N_log} log
     * @param {N_record} record
     * @param {N_search} search
     */
    (log, record, search) => {

        const GET = (requestParams) => {
            const { recordType, fieldId } = requestParams;
            const response = { success: false, data: [] };

            if (!recordType || !fieldId) {
                response.message = 'Missing recordType or fieldId parameters.';
                return response;
            }

            try {
                // Create a temporary record instance to get field values
                const tempRecord = record.create({ type: recordType, isDynamic: true });
                const field = tempRecord.getField({ fieldId: fieldId });

                if (field && field.getSelectOptions) {
                    const options = field.getSelectOptions();
                    response.data = options.map(option => ({
                        value: option.value,
                        text: option.text
                    }));
                    response.success = true;
                } else {
                    response.message = `Field '${fieldId}' not found or does not have select options for record type '${recordType}'.`;
                }

            } catch (e) {
                log.error({ title: 'GET_ERROR', details: e.message });
                response.message = `Error retrieving field options: ${e.message}`;
            }

            return response;
        };

        return { GET };
    });

RESTlet script to retrieve field select options.

Deploying and Testing the RESTlet

After creating the SuiteScript file, upload it to your NetSuite file cabinet. Then, create a new RESTlet deployment, linking it to your script. Ensure you select an appropriate execution role with the necessary permissions. Once deployed, you can test it using any HTTP client. The RESTlet expects two query parameters: recordType (e.g., 'salesorder', 'customer') and fieldId (e.g., 'entitystatus', 'customrecord_my_list_field').

curl -X GET \
  'https://YOUR_ACCOUNT_ID.restlets.api.netsuite.com/app/site/restlet.nl?script=YOUR_SCRIPT_ID&deploy=YOUR_DEPLOYMENT_ID&recordType=customer&fieldId=entitystatus' \
  -H 'Authorization: NLAuth nlauth_account=YOUR_ACCOUNT_ID, nlauth_email=YOUR_EMAIL, nlauth_signature=YOUR_PASSWORD, nlauth_role=YOUR_ROLE_ID' \
  -H 'Content-Type: application/json'

Example cURL command to call the RESTlet for customer status options.

A flowchart diagram illustrating the RESTlet process for retrieving field values. Start node 'External System Request' leads to 'RESTlet Execution (NetSuite)'. This branches to 'Validate Parameters' and then to 'Create Temporary Record (record.create)'. From there, it goes to 'Get Field Object (getField)' which leads to 'Call getSelectOptions()'. If successful, it maps options to 'JSON Response' and returns to 'External System'. Error paths are shown from validation and field retrieval to 'Error Handling & Response'. Use blue rectangles for processes, green diamonds for decisions, and red rectangles for error states. Arrows indicate flow.

RESTlet Process Flow for Field Value Retrieval

This RESTlet provides a flexible way to query field options dynamically. It's particularly useful for building forms in external applications that need to present up-to-date NetSuite field choices without hardcoding values or requiring manual updates. Remember that getSelectOptions() is only available for fields that are truly select lists. For other field types, you might need a different approach, possibly involving custom searches or parsing definitions if applicable.