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

};

});
Rookie Asked on October 25, 2020 in SuiteScript.
Add Comment
3 Answer(s)

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.

Intermediate Answered on October 26, 2020.
Add Comment

Attach a client script to your suitelet and use the fieldChanged function to redirect to the suitelet with different parameters.

use window.onbeforeunload = null;  – to get rid of the “changes may not get saved” message (or something like this)

Rookie Answered on October 27, 2020.
Add Comment

Here is an example – not tested

/**
* @NApiVersion 2.x
* @NScriptType Suitelet
* @NModuleScope SameAccount
*/
define([‘N/https’, ‘N/record’, ‘N/redirect’, ‘N/ui/serverWidget’, ‘N/search’, ‘N/log’, ‘N/runtime’],

function( https, record, redirect, serverWidget, search, myLog, runtime ){

function onRequest(context) {

var request = context.request;
var response = context.response;

if (request.method == ‘GET’) {

var getFilter = request.parameters.filter; //After the client script fieldchanged (Redirect) this parameter will be available

var form = serverWidget.createForm({
title: ‘Your Titel’
});

form.clientScriptFileId = yourFileId; //Attach your client script to the suitelet

var filter = form.addField({
id : ‘filter’,
type : serverWidget.FieldType.TEXT,
label : ‘filter’
});

if (getFilter){ //if getFilter has a value then set defaultValue
filter.defaultValue = getFilter;
}

/* Use a saved search….



context.response.writePage(form);
} else {
// POST method
}
}

return {
onRequest: onRequest
};
});

 

 

/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define([‘N/record’, ‘N/ui/dialog’, ‘N/search’, ‘N/ui/message’, ‘N/url’], function(record, dialog, search, message, url) {

function pageInit(scriptContext) {

//do something

}
function changeFilter(scriptContext) {
if (scriptContext.fieldId == ‘filter’){

var currentRecord = scriptContext.currentRecord;

var currentUrl = document.location.href;
var urls = new URL(currentUrl);

var getFilter = currentRecord.getValue({ //gets value from the Suitelet filter field
fieldId: “filter”
});

window.onbeforeunload = null;
window.location.replace(urls+’&filer=’+getFilter);

}
}
return {
pageInit: pageInit,
fieldChanged: changeFilter
};
});

Rookie Answered on October 29, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor