Add remove values based on list collector in the string field

Samiksha2
Mega Sage

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

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

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 👍

Thanks,
Anvesh

View solution in original post

3 REPLIES 3

AnveshKumar M
Tera Sage
Tera Sage

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 👍

Thanks,
Anvesh

Hi @AnveshKumar M ,

 

Thank you so much, 1st script is working. I missed one line. thats y it was not working.

 

Thanks,

Sam

@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;

}

 

 

Thanks,
Anvesh