My saved search results are looking like this in m...
# suitescript
c
My saved search results are looking like this in my reduceContext. Is there a way to force M/R to pull through the custom labels from the search instead?
s
switch to a SuiteQL search, or a Scheduled Script?
☝️ 1
another scheme is to use the
map()
phase to translate those into more meaningful names then pass them on to
reduce()
☝️ 1
c
Map solution sounds better than spending a large amount of time translating the search results into SuiteQL
Still a poor solution though as now we’re hardcoding these names into the script, removing control from the user that may change the formulas later.
s
If you need end users to change the search, consider a Scheduled Script. You didn't mention the use case but I see people just defaulting to a M/R script as if it's somehow a replacement for or automatically better than Scheduled Scripts. Since that is not the case, a scheduled script may result in a better solution for you.
c
The search has around 225,000 transactions.
All updated nightly
a
What I normally do and I will recommend is to use the native API methods to get those results into a proper format/structure, then you can pass them to any other stage with a consistent structure.
r
This function may be of use to you. I've pieced it together over time. It takes an
N/search.search
variable and returns an associative array. I added
useLabelForKey
for your use case, the object keys will be the column labels from the saved search, instead of the field names/joins.
Copy code
/**
     * Get all the results of a saved search returned in an Object array. Object properties are set to the column IDs
     * @param {Search} savedSearch The saved search object
     * @param {boolean} useLabelForKey Use the Saved Search Label instead of field name for the key
     * @param {boolean} getText Also get the text values for each result
     * @return {*[]}
     */
    const getAllResultsAsObjectArray = (savedSearch, useLabelForKey = false, getText = false) => {

        // Initialize the results array
        let results = [];

        // Run the search and iterate over the results
        savedSearch.run().each(function(result) {

            // Initialize the object
            let resultObj = {};

            // Loop over all the columns in the search and set the Object key/value pair
            savedSearch.columns.forEach(function(column) {

                // Set the key to either the column name or the label
                let key = column.name;
                if (useLabelForKey) {
                    key = column.label;
                } else if (column.join) {
                    key = column.join + '.' + key;
                }

                // build result entry with value of result
                let resultEntry = {
                    value: result.getValue(column),
                };
                // Optionally also add the text of the result if specified
                if (getText) {
                    resultEntry.text = result.getText(column);
                }
                resultObj[key] = resultEntry;

                return true;
            });

            // Push onto results array
            results.push(resultObj);

            return true;
        });

        return results;
    }