Skip Freemarker by Delivering Saved Search Results in a Suitelet

It took me a number of years working as a NetSuite developer to stumble onto this. It’s worth a look.

As a developer, you have the option of writing a saved search and creating your own custom advanced PDF/HTML template to print it. It’s a fantastic option. However, it also requires knowledge of the Freemarker templating language.

So here’s my alternative…

First, the saved search. Code it any way you like. Supply custom titles or not. There are no restrictions.

And here is the end-in-mind, a formatted table included in a Suitelet. Obviously, I’ve burred the data. Can’t share that. Sorry!

Here is now I read the results from the Saved Search.

And here is how I format the results. You’ll need an INLINE_HTML field on your form to hold the resulting markup. I’m counting on you knowing how to do that.

Here is the code in form you can cut and paste.

        function getIssues() {
            var issues = new Array();

            var mySearch = search.load({
                id: 'customsearch_sales_order_issues_2'
            });

            var myPages = mySearch.runPaged({ pageSize: 1000 });

            for (var i = 0; i < myPages.pageRanges.length; i++) {
                var myPage = myPages.fetch({ index: i });
                myPage.data.forEach(
                    function (result) {
                        var issue = {};
                        mySearch.columns.forEach(
                            function (col, index) {
                                issue['column_' + index] = { label: col.label, text: result.getText(col), value: result.getValue(col) }
                            }
                        )
                        issues.push(issue);
                    }
                );
            }
            return issues;
        }
        function formatIssues(issues) {
            var html = new Array();
            html.push('<table class="RPT">');
            html.push('<thead>');

            if (issues.length > 0) {
                var issue = issues[0];

                html.push('<tr>');
                for (var i = 0; i < 20; i++) {
                    if (issue.hasOwnProperty('column_' + i)) {
                        var sortType = isNaN(issue['column_' +i].text || issue['column_' +i].value) ? 'string' : 'float';
                        html.push('<th data-sort="'+sortType+'">' + issue['column_' + i].label + '</th>');
                    }
                }
                html.push('</tr>');
            }

            html.push('</thead>');
            html.push('<tbody>');

            issues.forEach(
                function (issue) {
                    html.push('<tr>');
                    for (var i = 0; i < 20; i++) {
                        if (issue.hasOwnProperty('column_' + i)) {
                            var vAlign = isNaN(issue['column_' +i].text || issue['column_' +i].value) ? 'left' : 'right';
                            html.push('<td align="' + vAlign + '">' + (issue['column_' + i].text || issue['column_' + i].value) + '</td>');
                            break;
                        }
                    }
                    html.push('</tr>');
                }
            )


            html.push('</tbody>');
            html.push('</table>');

            return html.join("\n");
        }

Whoops! Just noticed this… Here is a fix.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s