In a suitelet, there is a method called formField.addSelectOption that allows you to set the static select options for the select field. This is demonstrated by the sample code snippet below:

formField.addSelectOption({
	value: '1',
	text: 'Location A'
});
formField.addSelectOption({
	value: '2',
	text: 'Location B'
});
formField.addSelectOption({
	value: '3',
	text: 'Location C'
});
formField.addSelectOption({
	value: '4',
	text: 'Location D'
});
formField.addSelectOption({
	value: '5',
	text: 'Location E'
});
formField.addSelectOption({
	value: '7',
	text: 'Location F'
});

The following screenshots show what the select field woud look like.

Example # 1 of a suitelet select field
Example # 2 of a suitelet select field

There are common use cases wherein the options displayed by the suitelet field should come from a saved search. The sample code snippet would demonstrate how this is done.

In the following code, the use case is that the suitelet must have a field that dynamically lists down the saved searches in the system based on the “Transaction” record. This is not achievable via hard-coding the options, thus a function has to be created that performs a “search of saved searches”.

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

define(['N/runtime', 'N/record', 'N/search', 'N/config', 'N/ui/serverWidget' ],
function(runtime, record, search, config, ui) 
{
	function onRequest(context) 
	{
	
        if (context.request.method == 'GET'){
            var form = ui.createForm({ title: 'Select Field from Search' }); 
			
			var formField = form.addField({id: 'custpage_saved_searches',
				type: 'select',
				label: 'List of Transaction Saved Searches',
				container: 'salesorders'}); 
			
			var objSavedSearches = getSavedSearches(); 
			
				for (var i = 0; i < objSavedSearches.length; i++) {
					if (i == 0)
						formField.addSelectOption({
							value: "",
							text: "",
							});
						formField.addSelectOption({
							value: objSavedSearches[i].id,
							text: objSavedSearches[i].title,
							});
				}
			context.response.writePage(form); 
        } else {
            
        }
		
		function getSavedSearches() {
			var createSaveSearch = search.create({
				type: 'savedsearch',
				filters:
					[["recordtype", "anyof", "Transaction"]],
				columns: [
					search.createColumn({name: "internalid", label: "ID",}),
					search.createColumn({name: "title", label: "title", sort: search.Sort.ASC}),
				]
			})
			resultObj = []
			var saveSearchResult = createSaveSearch.run()
			saveSearchResult.each(function (item) {
				obj = {}
				obj.id = item.getValue({name: "internalid"})
				obj.title = item.getValue({name: "title"})
				resultObj.push(obj)
				return true
			})
			return resultObj; 
		}
	}

	return {
	 onRequest : onRequest 
	};
	
}); 

As you can see, the getSavedSearches function created a saved search that gets the Transaction saved searches in the system. It consolidated the data and return an object that is then used by this code block:

formField.addSelectOption({
		value: objSavedSearches[i].id,
		text: objSavedSearches[i].title,
});

CEANA Technology is a team of NetSuite Certified Developers with IT experience solely dedicated to NetSuite technologies for eight years. We are experts in both SuiteScript 2.0 and SuiteScript 1.0. Our team has worked with 400+ NetSuite scripts and workflows combined. You can review our project portfolio on this page.

Do you have NetSuite Development needs? Do you need help with a solution like this? Click on the button below and let’s have a chat about your project requirements!

Leave a Reply