RE: How to apply filter fields to Suitelet with Client Script
I’ve been building a Suitelet to display the result and columns dynamically from a saved search. Where I’m running into an issue is I need to be able to trigger an update to my sublist once the field has been set to update the filter on list. I’m ideally looking for something where I don’t have to trigger off of a button but if I need to I will.
Any help would be much appreciated.
/** * @NApiVersion 2.x * @NScriptType Suitelet */ define(['N/ui/serverWidget', 'N/search', 'N/runtime'], function(serverWidget, search, runtime) { function suiteletList(context){ if (context.request.method === 'GET') { var form = serverWidget.createForm({title: 'Transactions'}); var itemField = form.addField({ id: 'custpage_item' ,type: serverWidget.FieldType.SELECT, label: 'Item' ,source: 'item'}); var objSublist = form.addSublist({id: 'custpage_sublist1' ,type:serverWidget.SublistType.LIST ,label: 'sublist1'}); var objCurrScript = runtime.getCurrentScript(); var param_SavedSearchId = objCurrScript.getParameter({ name:'custscript_suitelet_search' }); var objSublistSearch = search.load({ id: param_SavedSearchId }); if(itemField!=null){ var fltrPFA = search.createFilter({ name:'item' ,operator:'IS' ,values: itemField }); objSublistSearch.filters.push(fltrPFA); } var SublistSearch = objSublistSearch.run(); var SublistSearchResults = SublistSearch.getRange(0, 1000); var c = 0; SublistSearch.columns.forEach(function(col){ c++; var colName = 'custpage_col' + c; if (col.label != 'nodisplay') { objSublist.addField({ id:colName ,label:col.label ,type: serverWidget.FieldType.TEXT }); } }); for (var ix = 0; ix < SublistSearchResults.length; ix++) { var result = SublistSearchResults[ix]; var c = 0; //loop through the columns to fill the data for (var k in result.columns) { c++; var colName = 'custpage_col' + c; if (result.columns[k].label != 'nodisplay') { var fieldValue; if ( result.getText(result.columns[k]) ){ fieldValue = result.getText(result.columns[k]) } else { fieldValue = result.getValue(result.columns[k]) }; if (!fieldValue) { fieldValue = ' '; } //add the value to the row / column objSublist.setSublistValue({ id:colName ,value:fieldValue ,line: ix }); } } } form.getClientScriptModulePath = './cs_script_sublist_search.js'; context.response.writePage(form); } } return { onRequest: suiteletList }; });
The normal flow for a suitelet is a GET to the suitelet which generates the initial form. You can then use a submit button which does a form POST which allows you to get the parameters set on the form. For your case, you could use the submit button to get the value of your item field and then write a new form.
If that is too unwieldy for you, then you can use a client script to get the value of the item field and then manipulate the sublist using the methods available on a currentRecord. This tends to be too slow for large numbers of sublist rows, it mimics the functionality of the ui too closely to be fast.
Hello Battik,
I am doing what you stated, I have fieldchanged event and then filed the sublist but I do not see the sublist getting an update
Here is my Suitelet sublist:
sublist = formobj.addSublist({
id: ‘custpage_item_assembly’,
type: ui.SublistType.LIST,
label: ‘Assembly Item’,
tab: ‘custpage_main_tab’
});
Client script code:
var counter= nlapiGetLineItemCount(“custpage_sublist_item”)+1;
nlapiSetLineItemValue(“custpage_sublist_item”, “sublist1”, counter, internalid);
nlapiSetLineItemValue(“custpage_sublist_item”, “sublist2”,counter, itemid);
nlapiCommitLineItem(“custpage_sublist_item”);