Expand your SuiteScript skills in the Sustainable SuiteScript community for NetSuite developers.

Subrecord Sneak Peek

Created: December 3, 2018

I'm wrapping up my preparation for recording the subrecord content today. Here's a sneak peek at some of the code you'll see. This is a User Event script that will automatically add the main Company address to every new Employee record:

function beforeSubmit (context) {
  log.audit({ title: 'Adding company address...' })
  addCompanyAddress(context.newRecord, getCompanyAddress())
  log.audit({ title: 'Company address added.' })
}

function getCompanyAddress () {
  log.audit({ title: 'Retrieving company address...' })

  var companyInfo = config.load({
    type: config.Type.COMPANY_INFORMATION
  })

  var companyAddress = companyInfo.getSubrecord({ fieldId: 'mainaddress' })

  return {
    addr1: companyAddress.getValue({ fieldId: 'addr1' }),
    addr2: companyAddress.getValue({ fieldId: 'addr2' }),
    addr3: companyAddress.getValue({ fieldId: 'addr3' }),
    city: companyAddress.getValue({ fieldId: 'city' }),
    country: companyAddress.getValue({ fieldId: 'country' }),
    state: companyAddress.getValue({ fieldId: 'state' }),
    zip: companyAddress.getValue({ fieldId: 'zip' })
  }
}

function addCompanyAddress (rec, address) {
  log.audit({ title: 'Creating new subrecord...' })

  rec.insertLine({ sublistId: 'addressbook', line: 0 })

  var newAddress = rec.getSublistSubrecord({
    sublistId: 'addressbook',
    fieldId: 'addressbookaddress',
    line: 0
  })

  newAddress.setValue({ fieldId: 'country', value: address.country })
  newAddress.setValue({ fieldId: 'state', value: address.state })
  newAddress.setValue({ fieldId: 'city', value: address.city })
  newAddress.setValue({ fieldId: 'zip', value: address.zip })
  newAddress.setValue({ fieldId: 'addr1', value: address.addr1 })
  newAddress.setValue({ fieldId: 'addr2', value: address.addr2 })
  newAddress.setValue({ fieldId: 'addr3', value: address.addr3 })
}

My challenge to you is to read/learn as much as you can from the code alone. What questions do you have? What is unclear from the code? Anything jump out at you?