SuiteScript – Transaction Statuses

I have been coding a solution that pulls together a given customer’s open invoices and formats them into a summary email and then attaches printed copies of each. One of the requirements was that I only include open invoices. Attaching PDFs of paid invoices was not only superfluous but also made generating the email take forever. So, I needed to filter to only include open invoices. Here’s how I accomplished this in SuiteScript.

This was the goal: a popup window that emails this report to a customer in the body of the email along with attached PDFs of all past due invoices. And just so you’ll know, I’m not lazy, the customer’s email and alternate email are actually populated in the actual popup. After reviewing the details, the user can adjust who they send to and either send the summary without attachments or include past due PDFs.

Popup Window

If you code your search as a Saved Search, you see something like this…

Saved Search

This is misleading and does not translate to an nlobjSearchFilter in SuiteScript. Here’s what your code should look like.

SuiteScript

So how did I make this translation? You’ll find the answer in SuiteAnswers #50165.

SuiteAnswers

Here is the code in a form more easily copied.

getSearchResults: function (customer_internalid) {
filters = new Array();
filters[0] = new nlobjSearchFilter(‘entity’, null, ‘is’, customer_internalid);
filters[1] = new nlobjSearchFilter(‘status’, null, ‘is’, ‘CustInvc:A’);
var columns =new Array();
columns[0] = new nlobjSearchColumn(‘tranid’);
columns[0].setSort(false);
columns[1] = new nlobjSearchColumn(‘trandate’);
columns[2] = new nlobjSearchColumn(‘duedate’);
columns[3] = new nlobjSearchColumn(‘amount’);
columns[4] = new nlobjSearchColumn(‘amountremaining’);
columns[5] = new nlobjSearchColumn(‘daysoverdue’);
columns[6] = new nlobjSearchColumn(‘internalid’);
var results = nlapiSearchRecord(null, ‘customsearch_logic_customer_ar_summary’, filters, columns);
return results;
}

One more note: If you look at the code sample I included, there is another function that might be of interest.

formatARStatusPage: function (customer_internalid, results) {
var renderer = nlapiCreateTemplateRenderer();
var template = nlapiLoadFile(‘SuiteScripts/Logic/customsearch_logic_customer_ar_summary.html’).getValue();
renderer.setTemplate(template);
renderer.addSearchResults(‘results’, results);
return JSON.stringify(renderer.renderToString());
}
This function marries the search results with a Freemarker template and formats (from a RESTlet) the HTML that you see in the popup window. Here is a snippet from the Freemarker code that formats the past due table.
Freemarker
<table>
<#list results as result>
<#if result_index==0>
<thead>
<tr>
<thcolspan=”6″style=”text-align: center”>Past Due Invoice – Please Pay Immediately</th>
</tr>
<tr>
<th>Document Number</th>
<thstyle=”text-align: center”>Invoice Date</th>
<thstyle=”text-align: center”>Due Date</th>
<thstyle=”text-align: center”>Days Overdue</th>
<thstyle=”text-align: center”>Invoice Amount</th>
<thstyle=”text-align: center”
style=”border-right: solid 1px #c9c9c9;”>Balance Due</th>
</tr>
</thead>
</#if>
<#if result.daysoverdue gt 0>
<tr>
<td>${result.tranid}</td>
<tdstyle=”text-align: center”>${result.trandate}</td>
<tdstyle=”text-align: center”>${result.duedate}</td>
<tdstyle=”text-align: center”>${result.daysoverdue}</td>
<tdstyle=”text-align: right”>${result.amount}</td>
<tdstyle=”text-align: right”
style=”border-right: solid 1px #c9c9c9;”>${result.amountremaining}</td>
</tr>
</#if>
</#list>
</table>

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