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

JC S_
Mega Guru

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:

  1. Created a new array called newEntries
  2. Check if an entry has value "comments" on its element
  3. - If YES, then check all items in the entry array if they contain a keyword then replace it with the replacement text
  4. - 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;

}

}

1 REPLY 1

cameronrichard
ServiceNow Employee
ServiceNow Employee

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