To add or remove IDs from a multi-select field in NetSuite using SuiteScript, you can manipulate the multi-select field using its internal ID and the respective value(s) you want to add or remove.
Here’s how you can approach both tasks:
1. Add ID to Multi-Select Field:
You would first retrieve the existing values from the multi-select field, append the new ID, and then set the field with the updated array of values.
// Assuming you're in a User Event Script or Suitelet
function addIdToMultiSelect(recordObj, fieldId, newId) {
// Get current values
let existingValues = recordObj.getValue({ fieldId: fieldId });
// Check if existingValues is null or undefined, initialize as an array if needed
existingValues = existingValues || [];
// Add the new ID if it's not already in the list
if (!existingValues.includes(newId)) {
existingValues.push(newId);
}
// Set the updated values back to the field
recordObj.setValue({
fieldId: fieldId,
value: existingValues
});
}
2. Remove ID from Multi-Select Field:
You would get the current values from the multi-select field, remove the specified ID, and update the field.
// Assuming you're in a User Event Script or Suitelet
function removeIdFromMultiSelect(recordObj, fieldId, removeId) {
// Get current values
let existingValues = recordObj.getValue({ fieldId: fieldId });
// Check if existingValues is null or undefined
if (existingValues && existingValues.length > 0) {
// Filter out the ID to remove
existingValues = existingValues.filter(function(value) {
return value !== removeId;
});
// Set the updated values back to the field
recordObj.setValue({
fieldId: fieldId,
value: existingValues
});
}
}
Example Usage:
let recordObj = record.load({
type: 'customrecord_my_record',
id: 12345
});
// To add ID 67890 to a multi-select field
addIdToMultiSelect(recordObj, 'custrecord_my_multiselect', '67890');
// To remove ID 67890 from the multi-select field
removeIdFromMultiSelect(recordObj, 'custrecord_my_multiselect', '67890');
// Save the record after modifications
recordObj.save();