I'm trying to make a script to put up on a display...
# suitescript
a
I'm trying to make a script to put up on a display, but I think the progress bar is throwing a wrench in things. I'm getting Error: Cannot find function includes in object WO47708 attached a example of the saved search for reference
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(['N/search'], function(search) {
    function onRequest(context) {
        if (context.request.method === 'GET') {
            try {
                var savedSearchId = '2953'; // Replace with your actual Saved Search ID
                var savedSearch = search.load({ id: savedSearchId });

                var resultSet = savedSearch.run();
                var results = resultSet.getRange({ start: 0, end: 50 }); // Adjust range as needed

                var html = '<html><head>';
                html += '<style>';
                html += 'body { font-family: Arial, sans-serif; }';
                html += 'table { width: 100%; border-collapse: collapse; }';
                html += 'th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }';
                html += 'th { background-color: #f2f2f2; }';
                html += '.progress { overflow: hidden; height: 18px; background-color: #ccc; border-radius: 4px; box-shadow: inset 0 1px 2px rgba(0,0,0,0.1); }';
                html += '.progress-bar { height: 100%; font-size: 11px; line-height: 18px; color: #fff; text-align: center; background-color: #428bca; box-shadow: inset 0 -1px 0 rgba(0,0,0,0.15); transition: width .6s ease; }';
                html += '</style>';
                html += '</head><body>';
                html += '<h2>WO Daily Plan Results</h2>';
                html += '<table>';
                html += '<tr>';

                if (results.length > 0) {
                    var columns = results[0].columns;

                    // **Generate Table Headers**
                    columns.forEach(function(column) {
                        html += '<th>' + column.label + '</th>';
                    });

                    html += '</tr>';

                    // **Generate Table Rows**
                    results.forEach(function(result) {
                        html += '<tr>';
                        columns.forEach(function(column) {
                            var value = result.getValue(column);

                            // **Handle non-string values (fix for the error)**
                            if (typeof value === 'object' && value !== null) {
                                value = result.getText(column) || '[Object]'; // Use getText() for objects
                            } else {
                                value = value !== null && value !== undefined ? String(value) : ''; // Convert everything to a string
                            }

                            // **Detect and render Progress Bar**
                            if (typeof value === 'string' && value.includes('%')) {
                                var percentage = parseFloat(value.replace('%', '')) || 0;
                                html += '<td><div class="progress"><div class="progress-bar" style="width:' + percentage + '%;">' + percentage + '%</div></div></td>';
                            } else {
                                html += '<td>' + value + '</td>';
                            }
                        });
                        html += '</tr>';
                    });

                    html += '</table>';
                } else {
                    html += '<p>No results found.</p>';
                }

                html += '</body></html>';

                context.response.write(html);
            } catch (e) {
                context.response.write('Error: ' + e.message);
            }
        }
    }

    return {
        onRequest: onRequest
    };
});
a
if (typeof value === 'string' && value.includes('%')) {
this is your problem, it doesn't like includes, it says so in your error message
it doesn't look wrong to me though... JS evaluates conditions left to right, so it should only be testing the includes against things that have already passed typeof === 'string'
but the error msg is suggesting it can't do an includes on an object, which is correct, it can't, includes is not an object method, but it shouldn't be getting that far with an object
¯\_(ツ)_/¯
10 4 1
a
I was hoping ChatGPT was just too dumb. Sad lol
a
oh yeah ok its chatGPT, its doing dumb AI things
🙌 1
its turning everything into a string
value = value !== null && value !== undefined ? String(value) : ''; // Convert everything to a string
a
is that due to the progress bar being a HTML formula
I'm real green on all this lol
e
includes
is not a method on the String prototype in SuiteScript 2.0 (ES5). You'll need to switch your script to 2.1, first by specifying the
@NApiVersion
correctly, then likely by re-creating the Script record.
☝️ 1
🙏 1
ty1 1
a
ok I'll give that a shot thank you
@erictgrubaugh ty1 ! is it safe to say using the newest script version is best practice?
e
That is what I prefer to do because I prefer the syntax of modern JS over the older version used by 1.0/2.0. I suppose it primarily comes down to your knowledge of JS and how much you want to take advantage of newer features.