Tuesday, September 25, 2018

Dynamically ADD select options to a SUBLIST SELECT field created via SuiteScript UI Builder APIs

The goal of NetSuite user is to dynamically 'ADD' select options to a sublist 'SELECT' field created via SuiteScript UI Builder APIs. Is wanted to source the values of the select field from a search. For this example, is needed to get all inventory items from a specific subsidiary and then make those items selectable on sublist select field under Suitelet form.

This solution needs a combination of a Client Side script and a Suitelet script. Below is a sample code that searches all Inventory Items from a specific subsidiary and then populate the select fields with the results of the search.
Sample Suitelet Script Code:
function main(request, response) {
 var form = nlapiCreateForm('Items', false);
 var sublist = form.addSubList('custpage_items', 'inlineeditor', 'Items');
 var itemField = sublist.addField('custpage_item', 'select', 'Item');
 form.setScript('customscript_populate'); //attach the client script code

Client Side Script Code:
function pageInit() {
 nlapiRemoveLineItemOption('custpage_items', 'custpage_item'); //remove all existing select options
    nlapiInsertLineItemOption('custpage_items', 'custpage_item', '', '', true); //Insert null value and default it to selected
 var filters = new Array();
 filters[0] = new nlobjSearchFilter('type',null,'is','InvtPart'); //Item type will be Inventory Item
 filters[1] = new nlobjSearchFilter('subsidiary',null,'is','1'); //Subsidiary is the first one
    var results = nlapiSearchRecord('item', null, filters, new nlobjSearchColumn('displayname')); // search all inventory items which are for a specific subsidiary
    for (var i in results)
        var id = results[i].getId();
        var name = results[i].getValue('displayname');
        nlapiInsertLineItemOption('custpage_items', 'custpage_item', id, name); //populate the sublist select field with options

The client script is deployed on the Page Init function of the Suitelet so that it populate the sublist select field upon load of the Suitelet form.

DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations. NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto. NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.

No comments:

Post a Comment