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

Redundant Department of Redundancy Department

Created: October 12, 2020

I love SuiteScript 2.0, and 2.1 moreso, but the design of the API can lead to some extremely repetitious, verbose code, e.g. :

var rec = r.create({ type: r.Type.CUSTOMER })
  .setValue({ fieldId: 'firstname', value: applicant.firstName })
  .setValue({ fieldId: 'lastname', value: applicant.lastName })
  .setValue({ fieldId: 'email', value: applicant.email })
  .setValue({ fieldId: 'companyname', value: applicant.company })
  .setValue({ fieldId: 'phone', value: applicant.phone })
  .setValue({ fieldId: 'isperson', value: 'T' })
  .setValue({ fieldId: 'category', value: enums.ConstituentCategory.INDIVIDUAL })
  .setValue({ fieldId: 'autoname', value: false })
  .setValue({ fieldId: 'entityid', value: generateEntityId(application, context.applicationId) })
  .save()

While I love that records provide a fluent interface, which lets us chain methods and avoid repeating the variable name, there's still a little left to be desired from the duplicated method calls (setValue, setValue, setValue, ...). The problem gets magnified when you're calling one of the more verbose methods over and over:

rec.setCurrentSublistValue({
  sublistId: 'item',
  fieldId: 'item',
  value: 123
})

Luckily, plain old JavaScript Arrays give us a lovely way of reducing our repetition and condensing this kind of code block. For all the fields we want to set, we construct an Array of the parameter Objects, and then use the Array's forEach method to invoke our setValue on each Object.

In SuiteScript 2.0:

var rec = r.create({ type: r.Type.CUSTOMER })
[
  { fieldId: 'firstname', value: applicant.firstName },
  { fieldId: 'lastname', value: applicant.lastName },
  { fieldId: 'email', value: applicant.email },
  { fieldId: 'companyname', value: applicant.company },
  { fieldId: 'phone', value: applicant.phone },
  { fieldId: 'isperson', value: 'T' },
  { fieldId: 'category', value: enums.ConstituentCategory.INDIVIDUAL },
  { fieldId: 'autoname', value: false },
  { fieldId: 'entityid', value: generateEntityId(application, context.applicationId) }
].forEach(function (f) { rec.setValue({ fieldId: f.fieldId, value: f.value }) })
rec.save()

In SuiteScript 2.1, it gets even more compact as we can take advantage of Array destructuring

const rec = r.create({ type: r.Type.CUSTOMER })
[
  ['firstname', applicant.firstName],
  ['lastname', applicant.lastName],
  ['email', applicant.email],
  ['companyname', applicant.company],
  ['phone', applicant.phone],
  ['isperson', 'T'],
  ['category', enums.ConstituentCategory.INDIVIDUAL],
  ['autoname', false],
  ['entityid', generateEntityId(applicant, context.applicationId)]
].forEach(([fieldId, value]) => rec.setValue({fieldId, value}))
rec.save()

The same pattern can be followed for many other common repetitious scenarios, like Search Filter/Column creation.

I've taken quite a liking to this pattern for condensing repeated method calls. How does it look to you?