This topic provides additional script examples for useful tasks.


Pick list

This example creates a pick list for choosing which customers to display on the page.

The callback variable is set to the function to be performed on the returned data. The read call on the CUSTOMER object requests the NAME field in JSON format. Finally, the select field provides a display point in the HTML.

<script>
var callback = function (responseText) {
    // Parse the response into a JavaScript object
    var CUSTOMER = JSON.parse(responseText);

    // Sort the array of results into alphabetical order by NAME
    CUSTOMER.sort((function (index) {
        return function (a, b) {
            return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
        };
    })('NAME')); // NAME is the sort by field name

    // Options variable to receive pick list values
    var options = '<option>-- Select Customer --</option>';

    // Add an option element for each customer record
    for (var i = 0; i < CUSTOMER.length; i++) {
        options += '<option value="' + CUSTOMER[i].NAME + '">' + CUSTOMER[i].NAME + '</option>';
    }

    // Load options in the 'pick_customer' element
    document.getElementById('pick_customer').innerHTML = options;
};
var s = new API_Session();
s.ip_read("CUSTOMER", "NAME", "", "json", "", callback);
</script>

<select name="select" size="1" id="pick_customer"></select>

GL Balance

This example returns a table for the GL Balance by Location for the account specified in the code. It is assumed there is a current reporting period with naming such as “Month Ended April 2014”.

<style>
    #scriptSection fieldset {
        padding: 30px;
        border: 1px solid #EEEEEE;
    }

    #scriptSection fieldset legend {
        font-weight: bold;
        padding-left: 20px;
        padding-right: 20px;
    }

    #scriptSection fieldset table td, #scriptSection fieldset table th {
        border-left: 1px solid #EEEEEE;
        padding-left: 20px;
        padding-right: 20px;
    }

    #scriptSection fieldset table th {
        border-bottom: 1px solid #EEEEEE;
    }

    #scriptSection fieldset table tr:hover td {
        background-color: #fEf98D;
    }
</style>
<div id="scriptSection">
<fieldset>
    <legend>Current Petty Cash Balance</legend>
    <div id="arGLBalanceHolder">
        <table>
            <thead>
            <tr>
                <td>Loading ...</td>
            </tr>
            </thead>
        </table>
    </div>
</fieldset>

<script>
jq(document).ready(function () {

    // Fetch and display the GL account balance
    var monthNames = ["January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"];
    var d = new Date();
    var month = monthNames[d.getMonth()];
    var year = d.getFullYear();
    var reportingPeriod = 'Month Ended ' + month + ' ' + year;
    var account = 1010;
    var api = new API_Session();

    var callback = function (data) {
        var holder = jq('div#arGLBalanceHolder');
        data = JSON.parse(data);
        if (data.length == 0) {
            holder.html('No data');
            return;
        }
        var tableContent = '<thead><tr><th>Account</th><th>Location</th><th>Amount</th></tr></thead><tbody>';
        for (var r = 0; r < data.length; r++) {
            tableContent += '<tr>';
            tableContent += '<td>' + data[r].ACCOUNTNO + '--' + data[r].ACCOUNTTITLE + '</td>';
            tableContent += '<td>' + data[r].LOCATIONID + '</td>';
            tableContent += '<td>' + Number(data[r].ENDBAL).toFixed(2) + '</td>';
            tableContent += '</tr>';
        }
        tableContent += '</tbody>';
        holder.html('<table>' + tableContent + '</table></br><p>(For ' + reportingPeriod + ')</p>');
    };

    api.ip_readByQuery(
        'glaccountbalance',
        'ACCOUNTNO,ACCOUNTTITLE,ENDBAL,LOCATIONID',
        "period = '" + reportingPeriod + "' AND accountno = '" + account + "'",
        10,
        'json',
        callback
    );
});
</script>
</div>

Pick List of Purchasing Transaction Types

<script>
var callback = function (responseText) {
    var xml = responseText,
        xmlDoc = jq.parseXML(xml),
        $xml = jq(xmlDoc);
    var select = jq('#pick_potransaction');

    $xml.find("type[typename='PODOCUMENT']").each(function () {
        var value = jq(this).text();
        if (value !== 'Purchasing Document') {
            select.append("<option value='" + value + "'>" + value + "</option>");
        }
    });

};
var s = new API_Session();
s.ip_inspect("*", "1", callback); // Returned as XML not JSON
</script>

<select name="select" size="1" id="pick_potransaction"></select>

User Restrictions loaded into DataTables

This example loads the first 1000 User Restrictions in the DataTables plugin.

You should place this script component on a custom Generic Menu Type page. Don’t forget to add https://cdn.datatables.net to the page’s Content Security Policy script, image, and style directives.

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

<table id="userRestrictions" class="display" width="100%"></table>

<script>
    jq(document).ready(function () {
        var callback = function (responseText) {
            var xmlDoc = jq.parseXML(responseText),
                $xml = jq(xmlDoc);

            dataSet = [];
            $xml.find("userrestriction").each(function() {
                var locations = [];
                jq(this).children("LOCATIONS").children("LOCATION").each(function() {
                    var locationId = jq(this).children("LOCATIONID").text();
                    var locationName = jq(this).children("LOCATIONNAME").text();
                    locations.push(locationId + "--" + locationName);
                });

                var departments = [];
                jq(this).children("DEPARTMENTS").children("DEPARTMENT").each(function() {
                    var departmentId = jq(this).children("DEPARTMENTID").text();
                    var departmentName = jq(this).children("DEPARTMENTNAME").text();
                    departments.push(departmentId + "--" + departmentName);
                });

                var row = {
                    "userId": jq(this).children("LOGINID").text(),
                    "firstName": jq(this).children("FIRSTNAME").text(),
                    "lastName": jq(this).children("LASTNAME").text(),
                    "admin": jq(this).children("ADMIN").text(),
                    "userType": jq(this).children("USERTYPE").text(),
                    "status": jq(this).children("STATUS").text(),
                    "unrestricted": jq(this).children("UNRESTRICTED").text(),
                    "locations": locations,
                    "departments": departments,
                };
                dataSet.push(row);
            });

            jq("#userRestrictions").DataTable({
                data: dataSet,
                paging: false,
                columns: [
                    {
                        data: "userId",
                        title: "User ID",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "firstName",
                        title: "First name",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "lastName",
                        title: "Last name",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "admin",
                        title: "Admin privileges",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "userType",
                        title: "User type",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "status",
                        title: "Status",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "unrestricted",
                        title: "Unrestricted",
                        render: jq.fn.dataTable.render.text(),
                    },
                    {
                        data: "locations[<br>]",
                        title: "Entities",
                    },
                    {
                        data: "departments[<br>]",
                        title: "Departments",
                    },
                ],
            });
        };

        var ia = new API_Session();

        ia.ip_readByQuery(
            "USERRESTRICTION",
            "*",
            "",
            1000,
            "xml",
            callback
        );
    });
</script>

Provide feedback