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.



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

Related posts

January 7, 2021

Anything I wrote today about business would both feel and be inadequate. I simply can’t arrive at the words to express how I feel in this moment. So instead, here are a few helpful ...

Read More

December 20, 2020

2020 was a hundred swift kicks to the shorts and one giant kick in the ass. If you’ve lost people you love, if you’ve gotten sick or hurt, if you’ve lost your job, if ...

Read More

December 16, 2020

“I’ve been meaning to learn SuiteScript, but it’s hard to find the time …”“We’re slammed, and I just don’t have the time …”“I haven’t made the time …” All are variations of a theme ...

Read More

December 14, 2020

Last week we looked at mimicking the functionality of a REST endpoint with its own module (e.g. retrieving a User, or a list of Users, refreshing a token, etc). For complex integrations that rely on a large ...

Read More