November 10, 2020

SuiteScript Design – The Singleton Record

For today’s SuiteScript Design Pattern, I’ll present how I create “Singletons” – though not the programmatic kind. Instead, we’re going to see how to force a Custom Record Type to be a Singleton. In other words, we’re only going to allow a single instance of a record type to exist.

I find integrations to be a great use case for this type of record – though it certainly has applications well beyond just integrations. Nearly any integration is going to require authentication, and most modern authentication relies on tokens which may or may not expire over time.

A Custom Record is a great place to store your token information for a specific integration, but each integration typically only needs one token, so we should only need a single instance of the record. In fact, if we allow multiple records of this Custom Record to exist, it might take a lot more work to figure out which one is current and correct, so let’s ensure that only one instance can ever exist.

  1. We need to allow the first instance to be created, but thereafter we need to prevent any additional record creations or copies.
  2. We need to prevent deletion of that first instance.

We can do all of this with a single User Event. You can find the full source code for this pattern here.

beforeLoad can be used to detect some new creations and copies:

and beforeSubmit can be used to prevent deletions and creations beyond the first:

Because we’ve made the record a singleton, we’ve guaranteed that the only instance of it will have an internal ID of 1. This assumption presents us with opportunities for many shortcuts like our isFirstRecord variable not having to do any sort of search.

The variable and function names should give a clear picture of the logic. I’ll leave it as an exercise for you to dig into the specifics of the smaller functions in the full source code.

HTH

-EG

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related posts

February 1, 2021

Around a decade ago now, I was spending most of my time with members of the United States Marine Corps. As an organization, the Corps invests significant time and energy maintaining a reputation as ...

Read More