- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 06:35 AM
Hi All,
I have created two fields in the Account table.
1. CSM- List-(User table)
2. CSMText- String
I have two scenarios:
1. When user selects users form CSM (list collector) and update the account form, value should populate in the CSMText field.
2. When user removes any user then it should remove the same value from the CSMText field.
I have created before BR on account table to copy the user
var list = current.u_csm;
var lstr = list.toString();
var arr = lstr.split(',');
for (var i = 0; i < arr.length; i++) {
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addQuery('sys_id', arr[i]);
grp.query();
if (grp.next()) {
temp = grp.name;
}
current.u_csmtext = current.u_csmtext + temp + ',';
}
current.update();
1 is working. how to achieve 2.
Also when there is no users in CSM field. how to remove ", " from the CSMText field.
Thanks,
Sam
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 08:22 PM - edited 01-27-2024 08:24 PM
Hi @Samiksha2
Instead of searching for a name in a string and removing it, you can simply update the list every time based on the current Selection in list collector. As you said, you are using the script in Before Business Rule, I removed current.update().
Try with the following code.
var list = current.getValue('u_csm');
var lstr = list.split(',');
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addEncodedQuery('sys_idIN' + lstr);
grp.query();
while (grp._next()) {
temp = temp + grp.getValue('name') + ', ';
}
if(temp){
temp = temp.substring(0, temp.length -2);
current.u_csmtext = temp;
}
If the above one didn't work, try the following one.
var list = current.getValue('u_csm');
var lstr = list.split(',');
for (var i = 0; i < lstr.length; i++) {
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addQuery('sys_id', lstr[i]);
grp.query();
if (grp.next()) {
temp = temp + grp.getValue('name') + ', ';
}
}
if(temp){
temp = temp.substring(0, temp.length -2);
current.u_csmtext = temp;
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 08:22 PM - edited 01-27-2024 08:24 PM
Hi @Samiksha2
Instead of searching for a name in a string and removing it, you can simply update the list every time based on the current Selection in list collector. As you said, you are using the script in Before Business Rule, I removed current.update().
Try with the following code.
var list = current.getValue('u_csm');
var lstr = list.split(',');
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addEncodedQuery('sys_idIN' + lstr);
grp.query();
while (grp._next()) {
temp = temp + grp.getValue('name') + ', ';
}
if(temp){
temp = temp.substring(0, temp.length -2);
current.u_csmtext = temp;
}
If the above one didn't work, try the following one.
var list = current.getValue('u_csm');
var lstr = list.split(',');
for (var i = 0; i < lstr.length; i++) {
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addQuery('sys_id', lstr[i]);
grp.query();
if (grp.next()) {
temp = temp + grp.getValue('name') + ', ';
}
}
if(temp){
temp = temp.substring(0, temp.length -2);
current.u_csmtext = temp;
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2024 08:54 PM - edited 01-27-2024 09:00 PM
Hi @AnveshKumar M ,
Thank you so much, 1st script is working. I missed one line. thats y it was not working.
Thanks,
Sam
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2024 05:13 AM
@Samiksha2 I'm glad that it helped. Actually I missed something that, there is no need to split the actual string. So you can also use the following code.
var list = current.getValue('u_csm');
var temp = '';
var grp = new GlideRecord('sys_user');
grp.addEncodedQuery('sys_idIN' + list);
grp.query();
while (grp._next()) {
temp = temp + grp.getValue('name') + ', ';
}
if(temp){
temp = temp.substring(0, temp.length -2);
current.u_csmtext = temp;
}
Anvesh