Suitescript Hook to fetch data from an Item saved search

In the saved search the columns include stock descriptions and Quantity available. By using the Suitescript Hook we can fetch these two data from the Item saved search.

The sample code for this is:

define(["N/search"],
    function (NSearch) {

        var preSendFunction = function (options) {
            // Load the saved search
            var searchObj = NSearch.load({
                id: 'Provide the ID of your Item saved search'
            });

            // Run the search and get the results
            var searchResults = searchObj.run().getRange({
                start: 0,
                end: 999
            });

            // Loop through the results and lookup the required fields
            _.each(options.data, function (itemSearch, index) {
                var result = searchResults[index];
                var lookupfieldValue = {
                    stockDescription: result.getValue({name: 'stockDescription'}),
                    quantityAvailable: result.getValue({name: 'quantityAvailable'})
                };
                itemSearch.stockDescription1 = lookupfieldValue.stockDescription;
                itemSearch.quantityAvailable2 = lookupfieldValue.quantityAvailable;
            })

            return {
                data: options.data,
                errors: options.errors
            }
          log.debug("saved search",data);
        }

        return {
            preSendFunction: preSendFunction
        }
    });

Leave a comment

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