In string field not taking second value, but in log i am getting two times
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 09:26 AM
Here in log "Names are "repeating two times, but while adding to the form in the watchlist and description I am getting only one user name, anything wrong in the code let me know
var grm = new GlideRecord('customer_account');
grm.addQuery('name', current.account.name);
grm.query();
gs.log('loop one' + current.account.getDisplayValue());
if (grm.next()) {
var grmh = new GlideRecord('customer_contact');
//var hd = grmh.addQuery('sys_tags.0470568187e58e1075d2a9f40cbb356a','=','0470568187e58e1075d2a9f40cbb356a');
var max = grmh.addQuery('sys_tags.0470568187e58e1075d2a9f40cbb356a', '=', '0470568187e58e1075d2a9f40cbb356a');
max.addCondition('account', '=', current.account);
//grmh.addEncodedQuery('account' + '=' + current.account + '^sys_tags.0470568187e58e1075d2a9f40cbb356a=0470568187e58e1075d2a9f40cbb356a');
grmh.query();
gs.log('loop two' + current.account.name);
// gs.log('Names are one' + grmh.name);
while (grmh.next()) {
gs.log('Names are two' + grmh.name);
current.watch_list = grmh.name;
current.description = grmh.name;
current.update();
}}
Can anyone help me with this how to keep two values to the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 12:13 PM
Whenever you set a value of a field, the value replaces the value that already exists.
It is not "combined" automatically, even for fields like lists which feel like those should.
To combine the values, you need to do it "manually":
- get the value already in the field into a variable
- append the new value to the variable
- set the value in the variable as value of the field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 12:33 PM - edited 04-20-2024 12:34 PM
What I mean is that instead of
current.watch_list = grmh.name;
you should have something like:
current.watch_list = appendItemToList(current.watch_list, grmh.name);
function appendItemToList (list, newItem) {
return String(list)
// Remove excess spaces from around the list that might exist
.trim()
// Break up the list where commas are found at the same time
// removing extra spaces that might exist around the commas
.split(/\s*,\s*/g)
// Add the new item to the list
.concat(String(newItem))
// Remove items that are empty string - e.g. splitting an
// empty list does not produce an array with no items, but
// produces an array with one item that is an empty string
.filter(keepNotNullAndNotEmpty)
// Remove duplicates - perhaps the newItem is already in the list
.filter(keepIfFirstOccurrence)
// Combine back the array into a comma separated list
.join(',')
}
function keepIfFirstOccurrence (item, itemIndex, items) {
return items.indexOf(item) == itemIndex;
}
function keepNotNullAndNotEmpty (item) {
return item != null && item != '';
}
Also you should NOT be adding names to fields of type "List".
The expectation is that these kind of fields will contain lists of values/items that don't contain commas - depending on how these lists have been defined, it would be sys_ids or choice values.
But names could contain commas.
E.g. Doe, Jane, Doe, John - which will not be interpreted as list
Doe, Jane
Doe, John
but as list
Doe
Jane
Doe
John
even if visually in the form it may look like it is what is expected.
So the instruction to update the watch list should be:
current.watch_list = appendItemToList(current.watch_list, grmh.sys_id);
The String(list) and String(newItem) expressions are necessary because watch_list and sys_id (in current.watch_list, grmh.sys_id and - as a consequence - list and newItem) are objects (not string/primitives containing the value in the underlying fields!) and thus they might not "contain" methods like trim or split (e.g. if the starting value of the list is no value).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 12:35 PM
Hi @-O- ,
My requirement is I need to show all the user names in description and list how to achive with script , i don't need manually
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2024 12:38 PM
"Manually" here does not mean literally, but that the function is not readily available - you must program it.
And I have also described it in detail.