Setting Saved search names to select option in Suitelet

Requirement

We need to populate the saved searches created in the account to the select option in the suitelet page. The saved searches of type customer is set in the select field which will allow us to choose from the field’s drop down options.

Solution

/** 
* @NApiVersion 2.1 
* @NScriptType Suitelet 
*/ 
define(['N/record', 'N/search', 'N/ui/serverWidget'], 
/** 
* @param{record} record 
* @param{search} search 
* @param{serverWidget} serverWidget 
*/ 
(record,search, serverWidget) => { 
/** 
* Defines the Suitelet script trigger point. 
* @param {Object} scriptContext 
* @param {ServerRequest} scriptContext.request - Incoming request 
* @param {ServerResponse} scriptContext.response - Suitelet response 
* @since 2015.2 
*/ 


//Function created to define the search for the saved searches of type customer.
 
function savedSearch() { 
var savedSearches = search.create({ 
type: search.Type.SAVED_SEARCH, 
filters: [ 
['recordtype', 'is', 'Customer'] 
], 
columns: [ 
search.createColumn({ 
name: 'id' 
}), 
search.createColumn({ 
name: 'title' 
}), 
search.createColumn({ 
name: 'recordtype' 
}) 
] 
}); 

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

return savedSearchRes; 
} 

const onRequest = (scriptContext) => { 

try { 

//Method type = GET 
if (scriptContext.request.method === 'GET') { 



//Creating a form 
var form = serverWidget.createForm({title: 'Statement Marketing and General Communication'}); 


var filterGrid = form.addFieldGroup({id: '_filter', label: 'Statement Details'}); 


//Invoking the saved search search 
var savedSearchRes = savedSearch(); 
log.debug('Saved search search res', savedSearchRes) 

//Field to select the search 
var selectSearchValue = form.addField({ 
id: 'custpage_savedsearch', 
type: serverWidget.FieldType.SELECT, 
label: 'Select Search', 
container: '_filter' 
}); 
selectSearchValue.isMandatory = true 
selectSearchValue.addSelectOption({ 
value: " ", 
text: " ", 
isSelected: true 
}) 

//Setting the saved search result names to the select option to select search 
for (var j = 0; j < savedSearchRes.length; j++) { 
selectSearchValue.addSelectOption({ 
value: savedSearchRes[j].id, 
text: savedSearchRes[j].getValue({ 
name: 'title' 
}), 
}) 
} 
} 

} catch (e) { 
log.debug('Error@Request', e) 
} 

} 

return {onRequest} 

}); 

Leave a comment

Your email address will not be published. Required fields are marked *