How to get specific item in a list of items and starting with value XYZ
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2022 12:11 PM
Hello,
I'm trying to get a specific item in a list of items and starting with a certain value XYZ.
The items are in a text field with the format: Item1,Item2,Item3,... (all items split by a comma)
I need to go through the list of items and find the one that starts with value OS_Family:
When I find the item I then need to look for a certain string (after the OS_Family:)
For example:
Item1,OS_Family:Windows,Item3,...
- Find the item starting with OS_Family:
- If item found look if the item contains the value Windows or value after : is Windows
Lastly this list of items comes into ServiceNow from an integration that uses a transform map, so this logic needs to be put in a field mapping script.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2022 12:35 PM
Maybe something like this could work:
//Replace "Name_of_Source_Field" with the real source field name.
//Basically the .split(",") will turn the string into an array
//With the array it will have the .filter method with which it will iterate through the items
//The item.match(/^OS_Family/) will see if the item starts with that pattern
//if so then it will return that item or items that match (hopefully you're sure only one will match)
if(typeof source.Name_of_Source_Field == "string"){
var theItem = source.Name_of_Integration_Field.split(',').filter(function(item){
return item.match(/^OS_Family/);
});
}
//Check if it found one
if(theItem.length > 0) {
//check if it Contains Windows
if(theItem[0].match(/Windows/i){
return theItem[0];
}
}
//Return an empty string if the source field isn't a string or "theItem.length" is 0 to account for no item matching found
return "";
This should be able to be used in a transform script.