Create a User Event Script to Print Child Custom Record Entries or other Child Subrecord within Advanced PDF/HTML Templates

Scenario

Currently there is no possibility to directly include child Custom Record entries in the printouts utilizing Advanced PDF/HTML Templates. This is being tracked under Enhancement 304767

As an alternate solution it is possible to create a beforeLoad User Event Script that will search over the custom subrecord entries and get the results into a custom field as JSON object. Then the Freemarker can parse the custom field content and build a HTML table in the printout. The steps how to establish that follow.

Under sales order there is a child custom record customrecord172 with several child record entries. User would like to print 3 columns (name, custrecord107 and custrecord112) of these records within a table as a part of the Sales Order printout.

Note: To search and vote for Enhancement see article 10054 Voting for Enhancements

Solution

Create a User Event Script

// SuiteScript 2.0
function customRecordPrint(context) {


    if (context.type == 'print' || context.type == 'edit') {
        var record = currentRecord.get();
        var recid  = record.id;


        //create saved search for custom records
        var filters = new Array();
        filters[0] = search.createFilter({ name: 'custrecord90',           operator: search.Operator.ANYOF, values: recid });


        var columns = new Array();
        columns[0] = search.createColumn({ name: "name"  });
        columns[1] = search.createColumn({ name: "custrecord107"  });
        columns[2] = search.createColumn({ name: "custrecord112"  });


        var mySearch = search.create({
            type: 'customrecord172',
            columns: columns,
            filters: filters
        });
        var results = mySearch.run();


        //populate current printout with custom record entries
        var customRecords = { columns: columns, results: result };
        var columns = customRecords.columns, results = customRecords.results,
        custrecord = form.addField({ id : 'custpage_custrecord_to_print',  type : serverWidget.FieldType.LONGTEXT, label : ''  }),
        custrecordArray = [];


        if (results && results instanceof Array) {
            for (var i = 0; i < results.length; i++) {
                var singleLine = {};
                for (var j = 0; j < columns.length; j++) {
                    var value = results[i].getValue(columns[j]);
                    if (j == 0 || j == 1 || j == 2) {
                        if (value.indexOf('.') == 0 || value.indexOf(',') == 0 || value.indexOf('-.') == 0 || value.indexOf('-,') == 0) {
                            value = '0' + value;
                        }
                    }
                    singleLine["col" + j] = (value) ? value : '';
                }
                custrecordArray.push(singleLine);
            }
            custrecord.setDefaultValue(JSON.stringify(custrecordArray));
        }
    }
}

Leave a comment

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