Sign up to receive SuiteScript examples and advice directly in your email inbox.

enum Example

Created: October 27, 2020

A reader asked for more detail on exactly how I leverage enumerations, so I thought I'd give a more explicit example.

An enumeration module is no different than any other custom module you would build, and it outputs a plain JavaScript Object, like so:

// my-enums.js
define([], () => ({
  Vendor: {
    Printful: 123
  }
}))

That is the enumeration module in its entirety. The structure is a plain JavaScript Object, so you can add as many properties as you want, and those properties can refer to numbers, strings, Arrays, other Objects, etc.

If I want to then reference that Printful property within, for example, a User Event script in the same directory as my-enums.js, it looks like this:

// my-user-event.js
define(['./my-enums'], (enums) => {
  function beforeSubmit (context) {
    const printfulId = enums.Vendor.Printful
    // ...
  }

  return { beforeSubmit }
})

While I would prefer enum over enums, be aware that enum is unfortunately a reserved keyword in JavaScript, and so cannot be used as a variable name.