ANS-0616 · SUITESCRIPT DEVELOPMENT
How to Accurately Compare NetSuite Text Area Fields in SuiteScript?
This guide provides a SuiteScript method for accurately comparing text area field content, like addresses, by normalizing strings with regular expressions.
Short answer
To accurately compare NetSuite text area fields in SuiteScript, use a regular expression to remove all whitespace and potentially non-visible characters like the enquiry character (x05). This normalization ensures that minor formatting differences do not prevent a correct comparison of the underlying text content.
Scenario
When comparing the content of text area fields in NetSuite, such as shipping addresses on a customer record, direct string comparison can be unreliable due to variations in whitespace or the presence of non-visible characters. This can lead to false negatives when attempting to determine if two text fields contain the same information.
Solution
To accurately compare text area fields, such as shipping addresses on a customer record, a robust SuiteScript technique involves normalizing the strings before comparison. This method accounts for variations in whitespace and other non-visible characters that might otherwise cause discrepancies. In the following example, newRecordShipAddress and oldRecordShipAddress are objects, and their respective .text properties hold the actual strings to be compared.
First, define a regular expression to target whitespace and the enquiry character:
javascript
var REGEX_REMOVE_SPACES_AND_ENQUIRY = /s|x05/g;The regular expression REGEX_REMOVE_SPACES_AND_ENQUIRY is designed to remove all whitespace characters (s) and, to ensure consistent comparison, potentially present non-visible characters like the enquiry character (x05). While the automatic presence of the enquiry character may vary depending on field configuration or NetSuite version, its removal helps standardize the strings for accurate comparison.
Next, process the new record's text:
javascript
var trimmedNewShipAddress = '';
if (newRecordShipAddress.text != null) {
trimmedNewShipAddress = newRecordShipAddress.text.replace(REGEX_REMOVE_SPACES_AND_ENQUIRY, '');
}Similarly, process the old record's text:
javascript
var trimmedOldShipAddress = '';
if (oldRecordShipAddress.text != null) {
trimmedOldShipAddress = oldRecordShipAddress.text.replace(REGEX_REMOVE_SPACES_AND_ENQUIRY, '');
}These trimmed strings can then be used for reliable comparison.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
