
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
09-29-2021 10:37 AM - edited 04-30-2024 03:04 PM
Script: Using split to use an array of SYS_ID's
Sometimes, we need to deal with a list of SYS_IDs in our scripts, whether it's for updating records or performing other operations. The JavaScript split() function is a useful tool for splitting a string into substrings based on a specific delimiter.
That's a little sample of code that we can use in FIX scripts for example, or be when we'd a list of sys IDs to update something on the record.
Go ahead and let me show a split function the W3 how us the sample code
var str = "How are you doing today?";
var myArr = str.split(" ");
Thats result an array where cut in a little pieces "how", "are", "you", "doing", "today?"
OR in our reality a list of sys_id's "b60426cb1bbaf4d0819396433b4bcb3e,e504a24bdbb6b4d00a66cf2414961973,eac3e28b1bbaf4d0c8f487bbe54bcbc1"
or also task numbers "CS5628163,CS5628164,CS5628165"when we use a split on this case we'll get each one separately on the same way before: the phrase
vIDD = "b60426cb1bbaf4d0819396433b4bcb3e,e504a24bdbb6b4d00a66cf2414961973,eac3e28b1bbaf4d0c8f487bbe54bcbc1";
var sysIdList = vIDD.split(',');
var numSel = sysIdList.length;
for (var i = 0; i < numSel; i++)
{
gs.log("(" + i + ") = " + sysIdList[i]);
}
and we'll get a great result to use update or another case that you'll need.
And thats our result
In this example, we have a string vIDD containing a list of SYS_IDs separated by commas. By using the split(',') function, this string is divided into an array called sysIdList, where each element of the array represents an individual SYS_ID.
The for loop then iterates over each element of the sysIdList array and prints the index of the element along with the value of the corresponding SYS_ID.
RESULT:
*** Script: sysIdList(0) = b60426cb1bbaf4d0819396433b4bcb3e
*** Script: sysIdList(1) = e504a24bdbb6b4d00a66cf2414961973
*** Script: sysIdList(2) = eac3e28b1bbaf4d0c8f487bbe54bcbc1
This method provides a convenient way to work with lists of SYS_IDs in scripts, allowing operations to be performed on each SYS_ID individually.
======================================================================
Exploring the Split Function in Javascript: A Comprehensive Guide
Introduction: In the realm of JavaScript programming, there are myriad tools and functions that empower developers to manipulate strings and arrays with ease and efficiency. Among these, the split()
function stands as a versatile tool for dividing a string into substrings based on a specified delimiter. In this article, we embark on a journey to unravel the intricacies of the split()
function, exploring its syntax, functionality, and practical applications in real-world coding scenarios.
Understanding the Split Function: The split()
function in JavaScript is a built-in method that allows developers to break a string into an array of substrings, using a specified delimiter as the separator. This delimiter can be a single character, such as a comma or a space, or even a regular expression pattern for more complex splitting requirements. By leveraging the split()
function, developers can effortlessly parse and process textual data, enabling a wide range of string manipulation tasks.
Syntax and Parameters: The syntax of the split()
function is straightforward, with the following format:
string.split(separator, limit);
- string: The original string to be split into substrings.
- separator: The delimiter used to identify the boundaries between substrings. This can be a string or a regular expression.
- limit (optional): An optional parameter that specifies the maximum number of substrings to be returned. If provided, the array will contain at most limit elements, with the final element containing the remainder of the string.
Practical Examples:
- Splitting a Comma-Separated String:
var str = "apple,banana,orange";
var fruits = str.split(",");
// fruits is now ["apple", "banana", "orange"]
- Splitting a Sentence into Words:
var sentence = "The quick brown fox jumps over the lazy dog";
var words = sentence.split(" ");
// words is now ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
- Using Regular Expressions:
var data = "apple,banana;cherry.orange";
var items = data.split(/[,;]/);
// items is now ["apple", "banana", "cherry", "orange"]
Best Practices and Considerations:
- Handle Empty Strings: Be mindful of cases where the string being split is empty, as this may result in unexpected behavior.
- Trimming Substrings: Consider trimming whitespace or unwanted characters from substrings obtained through splitting, especially when dealing with user input.
- Performance Considerations: For large strings or performance-critical applications, evaluate the efficiency of using
split()
versus alternative approaches to string manipulation.
Conclusion: The split()
function in JavaScript serves as a powerful tool for parsing and processing textual data, enabling developers to break down strings into manageable substrings with ease. By understanding its syntax, parameters, and practical applications, developers can leverage the split()
function to streamline string manipulation tasks and enhance the efficiency of their JavaScript code.
- 8,761 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
What I don't understand is when you add sys_ids to an array; why do you turn them into strings? Aren't they already a string?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@amol_joshi
this string "
vIDD = "b60426cb1bbaf4d0819396433b4bcb3e,e504a24bdbb6b4d00a66cf2414961973,eac3e28b1bbaf4d0c8f487bbe54bcbc1";
isn't
*** Script: sysIdList(0) = b60426cb1bbaf4d0819396433b4bcb3e
*** Script: sysIdList(1) = e504a24bdbb6b4d00a66cf2414961973
*** Script: sysIdList(2) = eac3e28b1bbaf4d0c8f487bbe54bcbc1
Sometimes, we need to deal with a list of SYS_IDs in our scripts, whether it's for updating records or performing other operations. The JavaScript split() function is a useful tool for splitting a string into substrings based on a specific delimiter.
In this example, we have a string vIDD containing a list of SYS_IDs separated by commas. By using the split(',') function, this string is divided into an array called sysIdList, where each element of the array represents an individual SYS_ID.
The for loop then iterates over each element of the sysIdList array and prints the index of the element along with the value of the corresponding SYS_ID.
This method provides a convenient way to work with lists of SYS_IDs in scripts, allowing operations to be performed on each SYS_ID individually.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Great post on the split command tiagomacul!
I find it useful in a variety of cases. A frequent one is when I need to break an DNS Name or IP address into its constituent parts/octets. The split('.') is a godsend then! Thank you for presenting the information in an accurate and easy to follow manner.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thanks @tiagomacul , I understand what you explained. I think I should have explained myself better previously. What I am trying to say is I have seen codes like:
var gr = new GlideRecord('sys_user');
gr.setLimit(5);
gr.query();
var array = [];
while(gr.next()){
array.push(gr.sys_id + '');
}
Now in the above code when they push the sys_id into an array, they try to convert it to string. I don't understand the purpose of it. Thanks
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@amol_joshi Sometimes things are strings, but not always. Sometimes you get a glide element, sometimes you get a reference, sometimes you get a glide variable. In your script example, if you didn't convert the sys_id to a string you'll be adding a reference to the glide record containing sys_id and you'll end up with an array where all of the values are the same!
var incref = new GlideRecord('incident');
incref.setLimit(3);
incref.query();
refarray = [];
while (incref.next()) refarray.push(incref.sys_id);
gs.info(refarray.join(','));
*** 46b66a40a9fe198101f243dfbc79033d,46b66a40a9fe198101f243dfbc79033d,46b66a40a9fe198101f243dfbc79033d
This is why you'll see people recommend "getters" and "setters," since they return a string. Things like gr.getValue('field_name') and gr.setValue('field_name', 'some_value') along with gr.getUniqueValue() to get the sys_id of the record.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Thank you @_ChrisHelming That was a great explanation.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
@_ChrisHelming definitely agree with everything you said. Also, if you end up with a GlideElement object, as Chris mentioned, you end up with using hundreds of times more memory per entry - this can quickly cause systemic performance impacts if you store large numbers of entries in an array or object. Wherever applicable, cast your dot-walked GlideRecord elements to JavaScript primitives with getters/setters or some other method.
@tiagomacul thanks for writing on the Array.split() method. I've written an article about using join/split vs. String concatenation in ServiceNow from a performance perspective here: https://www.servicenow.com/community/now-platform-articles/performance-best-practices-for-coding-com...