I have a restlet than when called is supposed to g...
# suitescript
p
I have a restlet than when called is supposed to generate a csv from a saved search. In the saved search I have the following criteria: ( Posting is true OR Status is Vendor Invoice: Pending Approval ) AND Account Type is any of Income, Cost of Goods Sold AND Period is Q1 2025 When i call the restlet I am only getting vendor invoices but in the UI I am getting Vendor Invoices, Credit Memos, Bill Credits, Invoice, Journal, Payments. Why is there a difference in results if I am calling the same saved search?
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType Restlet
 */
define(['N/search', 'N/log'], function(search, log) {

    /**
     * GET method for the RESTlet.
     * This RESTlet loads the saved search with ID 4140 and returns its results in CSV format.
     */
    function doGet(params) {
        try {
            // Load the saved search with ID 4140
            var savedSearchId = '4140';
            var mySearch = search.load({ id: savedSearchId });
            log.debug({ title: 'Saved Search Loaded', details: 'Search ID: ' + savedSearchId });

            // Run the saved search using paged results to handle large datasets
            var results = [];
            var pagedData = mySearch.runPaged({ pageSize: 1000 });
            pagedData.pageRanges.forEach(function(pageRange) {
                var page = pagedData.fetch({ index: pageRange.index });
                page.data.forEach(function(result) {
                    // Build an object for each search result using its columns
                    var columns = result.columns;
                    var rowData = {};
                    columns.forEach(function(col) {
                        var colKey = col.label || col.name;
                        rowData[colKey] = result.getValue(col);
                    });
                    results.push(rowData);
                });
            });
            log.debug({ title: 'Search Results Retrieved', details: 'Rows retrieved: ' + results.length });

            // Convert the JSON array to CSV format
            var csvOutput = convertToCSV(results);
            return csvOutput;
        } catch (e) {
            // Log the error details and return a simplified error message
            log.error({ title: 'Error in RESTlet', details: e });
            return { error: e.message };
        }
    }

    /**
     * Utility function to convert an array of objects into CSV.
     * It assumes a flat structure and encloses fields in quotes to handle commas.
     *
     * @param {Array} objArray - Array of objects representing search results.
     * @return {string} - CSV formatted string.
     */
    function convertToCSV(objArray) {
        if (!objArray || !objArray.length) {
            return '';
        }
        // Extract headers from the first object's keys
        var keys = Object.keys(objArray[0]);
        var csv = keys.join(',') + '\r\n';

        objArray.forEach(function(item) {
            var line = keys.map(function(key) {
                // Ensure null values are handled and any double quotes in data are escaped
                var value = item[key] != null ? item[key].toString().replace(/"/g, '""') : '';
                return '"' + value + '"';
            }).join(',');
            csv += line + '\r\n';
        });
        return csv;
    }

    return {
        get: doGet
    };
});