Don’t Miss it: Top Strategies for Driving Diversity

days / hours / minutes / seconds

Register Here »
NetSuite logo
NETSUITE RESOURCES
workplace by meta logo
FUTURE
OF WORK WORKPLACE BY META
University icon
CUSTOMER EDUCATION blog
Atlassian Logo
Adoption blog
How-to
September 21, 2023
|
5 min
reading time

SuiteScript 2.0: Suitelet Sample Code for Filtering the Values of a Dropdown Field

Share this article
/*SoMe buttons*/ <-- Facebook Share Button Code --> <-- Facebook Share Button Code --> <-- Twitter Share Button Code --> <-- Twitter Share Button Code --> <-- Linkedin Share Button Code --> <-- Linkedin Share Button Code -->

If you want to filter the values of a dropdown field in NetSuite SuiteScript, you’ll need two types of scripts. The first is the Suitelet Script itself. The second is the Client Script.

This blog breaks down the essential features of these two scripts and provides you with samples to help you better understand this process. Let's get started!

Step 1: The Suitelet Script

For the first step, copy the code below and deploy the Suitelet Script.

You’ll notice that we used the SuiteScript 2.0 API form.addField twice. We use it twice because we are adding two fields: one for the location and another for the vendor.

We also use another SuiteScript 2.0 API called form.clientScriptModulePath = ‘./lcc_cs_scripting_poc.js as this attaches the client script to the suitelet. This script is what controls the filtering behavior.

You can access the code here:



/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet
 */

define(['N/ui/serverWidget'], function(serverWidget) {

        function onRequest(context) {
          try {
            if (context.request.method == 'GET') {
                var form = serverWidget.createForm({ title: 'LCC POC' });

                form.addField({
                    id: 'custpage_location',
                    type: serverWidget.FieldType.SELECT,
                    label: 'Location',
                    source: 'location'
                });

                form.addField({
                    id: 'custpage_vendor',
                    type: serverWidget.FieldType.SELECT,
                    label: 'Vendor',
                    source: 'vendor'
                });
                
                form.clientScriptModulePath = './lcc_cs_filter_select_field.js';
                context.response.writePage(form);
            } else {
               
            }

          } catch(e) {
            log.debug('onRequest:error',e);
          }
        }

        return {
            onRequest: onRequest
        };


    });

Step 2: The Client Script

For the second step, you must upload the .js file to the File Cabinet to attach the Client Script to the Suite Script. Again, the sample code is available below for you to copy. This script is what you will put in your .js file.

Something to be mindful of is ensuring that the client script's directory is the same as what you indicated on the form.clientScriptModulePath API.

In addition, you will see that we used vendfield.insertSelectOption API to add the dropdown options based on the select field.

You can access the code here:



/**
 * @NApiVersion 2.0
 * @NScriptType ClientScript
 * @NModuleScope Public
 */

define(['N/search', 'N/currentRecord'], function(search,currentRecord) {
    var record = currentRecord.get();

    function fieldChanged(context) {
      if(context.fieldId == 'custpage_location') {
        var location = record.getValue('custpage_location');

        if(location == '') return;
        
        var vendorSearch = search.create({
          type: 'vendor',
          filters: [['custentity_location','is',location]],
          columns: ['entityid']
        });

        var vendorSearchResults = vendorSearch.run().getRange({ start: 0, end: 1000 });

        var vendfield = record.getField('custpage_vendor');
        vendfield.removeSelectOption({value : null});
        if(vendorSearchResults.length != 0) {
          for(var i in vendorSearchResults) {
            vendfield.insertSelectOption({
                value : vendorSearchResults[i].id,
                text : vendorSearchResults[i].getValue('entityid')
            });
          }
        }
      }
    }

    return { fieldChanged : fieldChanged };

});

You have to ensure that the directory of the client script is exactly what you indicated on the form.clientScriptModulePath API.

You will see that we used vendfield.insertSelectOption API to add the dropdown options, which are based on the select field. Now that you have deployed these scripts, you can filter the values of a dropdown field in NetSuite SuiteScript.

Want to Get Even More Out of NetSuite?

With ServiceRocket, you’ll get certainty, expertise and success built into your NetSuite implementation, configuration and custom development. When you work with us, you get access to NetSuite-trained Certified Developers with over 400+ NetSuite scripts and 50+ Suitelet workflows combined. Rest assured that ServiceRocket will help you find success and provide you with the opportunities you need to transform your business.

Do you have NetSuite license management, implementation, administration, support or custom development needs? We’ve got your back! Contact our NetSuite Certified Experts today.

Ensure the highest return on your NetSuite investment with our custom development or administrative services.

Learn MoreServiceRocket Backed - We've got your back badge