We can create a saved search inside the suite script and run it using the ‘N/search’ module functions. But there can be instances where the search result should be fetched by
running an existing saved search present in the NetSuite account. This is when the user wants to update the saved search conditions over time without modifying the script.
There are 3 methods of doing this:
- Hardcoding the internal id of the saved search inside the suite script.
We can hardcode the internal id of the saved search inside the script itself or the common library file used in the script implementation with other constant values. - Storing the saved search as a custom field value
The saved search can be stored in a custom field, and this is more user-friendly. For this create a custom field with ‘Type’ as ‘List/Record’ and the ‘List/Record’ as ‘Saved search’. We can add this field anywhere in a related standard record or custom record used in the functionality. Either keep it as ‘Disabled’ or limit the access to the field to certain users to avoid accidental changes.
To use the saved search, load the record and fetch the field value using the ‘record.getValue()’ function(Make sure to use the N/record module). We will get the internal id of the search in this way.
Load the search by using search.load() function in N/search module. Refer to the code provided. - Storing the saved search as a script parameter
The saved search can be stored as a script parameter in the script used. This approach is more developer-friendly and can be done by creating a script parameter with type as ‘List/Record’ and choosing the list as ‘Saved Search’. To fetch the saved search id inside the script use the script object and scriptObject.getParameter() function from the ‘N/runtime’ module.
var scriptObj = runtime.getCurrentScript();
var orderSearchId = scriptObj.getParameter({
name: 'custscript_monthly_order_search'
});
orderSearchObj = search.load({
id: orderSearchId
});