Saturday, July 6, 2019

How to source fields for Advanced PDF/HTML Templates using SuiteScript

Some fields are not directly accessible in Advanced PDF/HTML Templates. When it is not possible to source these fields with basic customization a SuiteScript User Event can be used to calculate these fields on the fly for printing. When a new field is created in nlobjForm for the record, it does not have to be stored and can be used by printing forms.

In the following example, we will source Customer Primary Contact for printing on Sales Orders:

1) Deploy following script on Sales Order, using its Before Load trigger:

function userEventBeforeLoad(type, form, request){

      var primaryContact = 'No contact defined'; // Backup option if Primary contact is not found


      var customerId = nlapiGetFieldValue('entity'); // Customer Internal ID for current record

      if (customerId != null) {

            var customerRec = nlapiLoadRecord('customer', customerId); // Load customer record


            // Search Customer record for Primary Contact

            for (var i = 1; i <= customerRec.getLineItemCount('contactroles'); i++) {

                  var contactRoleId = customerRec.getLineItemValue('contactroles', 'role', i);

                  var contactName = customerRec.getLineItemValue('contactroles', 'contactname', i);


                  if (contactRoleId == 14) { // If Contact Role == 14, it is Primary Contact

                        primaryContact = contactName;





      // Add form field custpage_primarycontact containing Primary Contact name

      var contactField = form.addField('custpage_primarycontact', 'text', 'TMP Primary Contact');


      contactField.setDisplayType('hidden'); // Hide the field so that it does not show up in the UI


The script will calculate Customer Primary Contact and will place in on the Sales Order form. The field is then accessible by printing forms.

2) Use the new field in Printing layout with following code:


Detailed description of nlobjForm can be found in SuiteAnswers #10266

No comments:

Post a Comment