KPI Portlet Script Sample

/**
 * @NApiVersion 2.x
 * @NScriptType Portlet
 */
define(['N/search', 'N/format'], function(search, format) {


    function render(params) {
        var portlet = params.portlet;
        portlet.title = 'Key Performance Indicators';


        var totalSales = 0;
        var totalCustomers = 0;


        // Calculate total sales
        var salesOrderSearch = search.create({
            type: search.Type.SALES_ORDER,
            filters: [['mainline', 'is', 'T']],
            columns: ['total']
        });


        salesOrderSearch.run().each(function(result) {
            totalSales += parseFloat(result.getValue('total'));
            return true;
        });


        // Count total customers
        var customerSearch = search.create({
            type: search.Type.CUSTOMER,
            columns: ['internalid']
        });


        totalCustomers = customerSearch.runPaged().count;


        // Format the numbers
        totalSales = format.format({
            value: totalSales,
            type: format.Type.CURRENCY
        });


        // Display the results
        var html = '<table>';
        html += '<tr><th>KPI</th><th>Value</th></tr>';
        html += '<tr><td>Total Sales</td><td>' + totalSales + '</td></tr>';
        html += '<tr><td>Total Customers</td><td>' + totalCustomers + '</td></tr>';
        html += '</table>';


        portlet.html = html;
    }


    return {
        render: render
    };
});


Leave a comment

Your email address will not be published. Required fields are marked *