Using an Enums Custom Library, Part 2

NetSuite for Developers

Using an enums custom library in SuiteScripts can drastically increase the efficiency of our scripts. Last time, we explained some of the foundational concepts behind using an enums custom library. Today, we’ll dive into some ways to actually implement this new efficiency into our projects.

Creating Fields with a Loop

With an enums module, we can create an array with all the information we need.

sublist_def: {
    id: 'custpage_po_sublist', label: 'Open Purchase Orders', fields: [
        { id: po_cust_ids.PO_LINE_INTERNAL_ID, mappedField: 'internalid', label: 'Internal ID', type: 'text', displayType: 'hidden', summary: 'group', editable: false },
        { id: po_cust_ids.PO_LINE_TRAN_ID, mappedField: 'tranid', label: 'Purchase Order', type: 'text', summary: 'group', editable: false },
        { id: po_cust_ids.PO_LINE_SUPPLIER, mappedField: 'companyname', join: 'vendor', label: 'Supplier', type: 'text', summary: 'group', editable: false },
        { id: po_cust_ids.PO_LINE_SUPPLIER_PHONE, mappedField: 'phone', join: 'vendor', label: 'Supplier Phone', type: 'text', summary: 'group', editable: false },
        { id: po_cust_ids.PO_LINE_PURCHASE_DATE, mappedField: 'trandate', label: 'Purchase Date', type: 'text', summary: 'group', editable: false },

        // A ton more fields can go here too! ...
    ]
}

For each field that we are creating (or for a similar task that can be looped), we can create an object within that array (or an object within an object if desired).

In the above example, we created a sublist for a Suitelet. Note that within each object, we also call other enums within the same file (particularly for the field IDs in this example).

Structuring our project this way reduces the number of times we need to create a field or perform other similar automations. Although this method can be revolutionary for every script, it is especially helpful for creating Suitelets. No longer do we need to manually create field after field after field. We can simply put it in a loop!

for (var i = 0; i < sublist_def.fields.length; i++) {
    var field = sublist.addField({
        id: sublist_def.fields[i].id,
        label: sublist_def.fields[i].label,
        type: sublist_def.fields[i].type,
        source: sublist_def.fields[i].source ? sublist_def.fields[i].source : null
    })
    if (sublist_def.fields[i].displayType) {
        field.updateDisplayType({ displayType: sublist_def.fields[i].displayType })
    }
}

As you can see, this is quite a bit simpler! If there are some parameters that vary between the fields, we tend to use ternary operators whenever possible. In most cases, null is a valid value since the values are already null by default.

But in some cases, such as in setting the displayType, null is not a valid value. In this case with displayType, we have two options. One option is to use a ternary operator and set the standard value to normal. The second option is to simply check if the enums definition has a key called “displayType.” If so, then we will update the field. In all other cases, this automation will be ignored.

Mapping Fields

If you notice in the enums example, we have a key called “mappedField.” This can come in handy when trying to populate the fields you created. Say for example that we want to create a field in a Suitelet called “Total Items on PO” and have it source from the PO’s quantity. We can add that connection to the existing field here in our “mappedField” object key. This way, we can populate our fields as easily as we created them.

// ... Some looping code here, likely a search.

if (fields[i].mappedField) {
    var fieldValue = result.getValue({
        name: fields[i].mappedField,
        join: fields[i].join ? fields[i].join : null,
        summary: fields[i].summary ? fields[i].summary : 'group'
    });
}

Conclusion

We hope that this might open some eyes to new possibilities. Using an enums custom library is not just a small improvement that you can make in your scripts; it is absolutely transformative. Previous projects that once were long and complex can now be reduced to small script files.

If you have any questions or suggestions about using enums, please feel free to get in touch with us or leave a comment! We would love to help any way we can and are always wanting to learn more. Remember to subscribe to our email list to stay up to date with our latest NetSuite developer blogs.