Script To Update Multi-Select Field on Save

Written by
Manuelito Macalinao
Published on
November 30, 2023 at 7:43:02 AM PST November 30, 2023 at 7:43:02 AM PSTth, November 30, 2023 at 7:43:02 AM PST

Scenario

The article applies to a very specific scenario, but it also demonstrates how to useSuiteScriptto set values on Multi-Select Fields in NetSuite.

The following solution applies to a Scenario where on the Support Case Record it would be necessary to track down on a single field all the employees to whom the same Case has been escalated. As a new Employee is added to the Subtab Escalations > Sublist Escalated To upon saving the record the new Escalatee will be added to the Multi-Select Field that keeps track of all Employees.

Solution

Before achieving the mentioned goal viaSuiteScript, firstly is necessary to create a Custom Multi-Select Field:
  1. Navigate toCustomization>List, Records & Fields>CRM Fields
  2. ClickNew
  3. Label the fieldEscalatees List
  4. Set your own fieldCustom ID
  5. Set theTypetoMultiple Select
  6. SetList/RecordtoEmployee
  7. On theApplies Tosubtab checkCase
  8. ClickSave
Deploy the followingSuiteScript 2.0on the Support Case Record using a Server-SideUser Event Scriptusing the Record Module onAfter Submitcontext:
/** * @NApiVersion 2.0 * @NScriptType UserEventScript * @NModuleScope SameAccount */define(['N/record'],/** * @param {record} record */function(record){/** * Function definition to be triggered before record is loaded. * * @param {Object} scriptContext * @param {Record} scriptContext.newRecord - New record * @param {Record} scriptContext.oldRecord - Old record * @param {string} scriptContext.type - Trigger type * @Since 2015.2 */functionsortEscalatees(scriptContext){try{var supportCaseId = scriptContext.newRecord.id;var oldEscalatees =newArray();var newEscalatees =newArray(); log.debug({ title:'Record ID', details: supportCaseId });var supportCase = record.load({ type: record.Type.SUPPORT_CASE, id: supportCaseId, isDynamic:true}); oldEscalatees = supportCase.getValue({ fieldId:'CRM_FIELD_CUSTOM_ID'}); log.debug({ title:'oldEscalatees', details: oldEscalatees });if(oldEscalatees !=null){for(var i =0; i < oldEscalatees.length; i++){ newEscalatees.push(oldEscalatees[i]);}} log.debug({ title:'newEscalatees', details: newEscalatees });var escalateToCount = supportCase.getLineCount({ sublistId:'escalateto'}); log.debug({ title:'escalateToCount', details: escalateToCount });for(var i =0; i <= escalateToCount -1; i++){var escalatee = supportCase.getSublistValue({ sublistId:'escalateto', fieldId:'escalatee', line: i }); newEscalatees.push(escalatee); log.debug({ title:'escalatee', details: escalatee });} log.debug({ title:'newEscalatees', details: newEscalatees }); supportCase.setValue({ fieldId:'CRM_FIELD_CUSTOM_ID', value: newEscalatees }); supportCase.save();}catch(error){ log.debug({ title:'Catch Error', details: error });}}return{ afterSubmit: sortEscalatees };});