Is there a limitation to the number of results tha...
# suitescript
j
Is there a limitation to the number of results that I can fetch via suitescript on saved searches? I have a restlet script that gets called to get saved search results based on the saved search ID passed.
Copy code
define(['N/runtime',
    'N/search',
    'N/record',
    'N/task',
    'N/file'
], function(runtime, search, record, task, file) {
    
    function get(context) {
        const searchResult = fetchSearchResults(filterData);
        return JSON.stringify(searchResult);
    }

    const fetchSearchResults = (filterData) => {
        const { savedSearchId, startDate: startDateTime, endDate: endDateTime, rows, force } = filterData;
        let searchObj = {};
        try {
            searchObj = search.load({
                id: savedSearchId
            });
        } catch (e) {
            return {
                error_code: 400,
                details: `Saved Search ID not found: (${savedSearchId})`
            };
        }
        const results = getResults(searchObj.run(), rows);
        return results.map(mapSearchResults);
    }
   
    const getResults = (savedSearchObj, rows) => {
        let holder = [];
        const limit = 100000;
        let i = 0;
        while (true) {
            if (holder.length == limit) break;
            const result = savedSearchObj.getRange({
                start: i,
                end: i + 1000
            });
            if (!result) break;
            holder = holder.concat(result);
            if (result.length < 1000) break;
            i += 1000;
            log.emergency({
                title: `get Results data ${i + 1}`,
                details: {
                    notResult: !result,
                    resultLengthLessOneK: result.length < 1000,
                    i: i
                }
            });
        }
    };

    const mapSearchResults = (result) => {
        const columns = result.columns;
        const obj = {};
        columns.forEach((column, idx) => {
            const textData = result.getText(columns[idx]);
            const valueData = result.getValue(columns[idx]);
            obj[column.label] = !!textData ? textData : valueData;
        });
        return obj;
    };

    return {
        get
    }
});
I always get
Copy code
UNEXPECTED_ERROR	|     An unexpected SuiteScript error has occurred
when trying above 30k results
b
probably would need to figure out which line is throwing the error
my guess here is that it fails at stringifying 30k search results
id also consider rewriting your while loop, its traditional to put your conditions inside the parenthesis of the while statement
using getRange also more closely fits a do while loop
s
or try NFT, where all that code is roughly
Copy code
return Seq(LazySearch.load(searchid)).map(nsSearchResult2Obj()).toArray()