ANS-0543 · SUITESCRIPT DEVELOPMENT

How to Programmatically Load, Edit, and Save a NetSuite Saved Search

Learn how to dynamically modify and persist changes to a NetSuite saved search using SuiteScript 2.x.

Short answer

To programmatically load, edit, and save a NetSuite saved search, use the N/search module in SuiteScript 2.x. Load the search with search.load(), add or modify filters using search.createFilter() and the filters array, and then persist changes with search.save(). This functionality is supported in both client-side and server-side scripts.

Scenario

A NetSuite user needs to dynamically update the filters of an existing saved search and save these modifications programmatically. The goal is to ensure that the saved search reflects the new criteria for future use or reporting.

Solution

Programmatically loading, modifying, and saving a NetSuite saved search is achievable using SuiteScript 2.x. Unlike previous versions, search.Search.save() is supported in client scripts, allowing for dynamic updates to saved search definitions from both client-side and server-side contexts. To implement this, follow these steps:

  1. Load the Saved Search

    Utilize the search.load() method from the N/search module to retrieve the existing saved search by its ID.

  2. Modify Filters

    Add new filters or modify existing ones by pushing search.createFilter() objects to the filters array of the loaded search object.

  3. Save Changes

    Call the save() method on the search object to persist the modifications to the saved search definition.Here is an example demonstrating how to load a saved search, add a filter, and save it using SuiteScript 2.x:

javascript
/**
 * Example snippet for a SuiteScript 2.x User Event script's beforeSubmit function.
 * Ensure the N/search module is defined in your script's define statement.
 */
// Assuming 'search' module is loaded, e.g., define(['N/search'], function(search) { ... });
function beforeSubmit_test(scriptContext) {
    var mySearch = search.load({
        id: 'customsearch376'
    });
    mySearch.filters.push(search.createFilter({
        name: 'vendor',
        operator: search.Operator.ANYOF,
        values: [9464]
    }));
    mySearch.save();
}

When using the save() method, both parameters are optional, and the original values from the loaded search will be retained if not explicitly overridden.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant