How to replace contents of an item in an array (Service Portal widget)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2017 12:39 AM
We are trying to modify the displayed Name of user as shown on the Ticket Conversations widget. This display name is part of an array. What we want is for example, if the display name contains a period then replace that period with a space.
So far, what we did is below:
- Created a new array called newEntries
- Check if an entry has value "comments" on its element
- - If YES, then check all items in the entry array if they contain a keyword then replace it with the replacement text
- - If NO, then just push all items in entry array to newEntries array
However we are getting an error that indexof is not a function. We need help on the text replace part since that's the part that is currently not working.
Here's the relevant part on our Client Script:
function mergeStreamEntries() {
$scope.placeholder = $scope.data.placeholderNoEntries;
if (!$scope.data.stream || !$scope.data.stream.entries)
return;
$scope.placeholder = $scope.data.placeholder;
// MODIFY NAME DISPLAYED ON TICKET CONVERSATIONS
var newEntries = [];
$scope.data.stream.entries.forEach(function(entry) {
if (entry.element == 'comments') {
var hey = entry.indexOf('keyword');
if (hey !== -1) {
entry[hey] = 'replacement text';
}
newEntries.push(entry);
} else {
newEntries.push(entry);
}
});
$scope.data.stream.entries = newEntries;
// END OF RELEVANT CODE
var entries = $scope.data.stream.entries;
if (!$scope.data.mergedEntries) {
$scope.data.mergedEntries = $scope.data.stream.entries.slice();
for (var i = 0; i < entries.length; i++) {
existingEntries[entries[i].sys_id] = true;
}
return;
}
var mergedEntries = $scope.data.mergedEntries;
for (var i = entries.length-1; i >= 0; i--) {
var curEntry = entries[i];
if (existingEntries[curEntry.sys_id])
continue;
mergedEntries.unshift(curEntry);
existingEntries[curEntry.sys_id] = true;
}
}
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2017 03:11 AM
Hi Jimboy,
On line 14, where you're trying to do an indexof, it looks like the object you're acting upon is not a String.
On line 12, if (entry.element == 'comments') {, you're referencing "entry" like an object; in this case, getting the element name.
For your indexof to work, you would need to get a String value out of your entry object. Something like entry.name.indexOf...
Does that help?
Thanks,
Cameron