Populate users with comma separated in a String field

Lucky1
Tera Guru

Hi folks,

on the request form, there are 2 fields, "Requester" which is of type reference and "Users List" which is of type String. When i select a user in the requester then in the Users List field, his user ID should populate and the Requester field should get cleared. Another time if you select other user, then the Users List field should populate with that value too with comma separated. 

Note: The user which we have already selected should not populate again when we try to select him again. It should give alert like "you have already selected the user"

 

So for my above query, i am showing an example here:

If i select Abel Tuter in Requester field, then the Users List field should populate with Abel.Tuter, Now if you select Dean james then the Users List field should look like Abel.Tuter,Dean.Jones

Like this the Requester field should be cleared and the selected users should populate in the Users List field with comma separated. No duplicates should come. 

I tried with get Reference method, and i am able to populate Users List field with only one value. I think we have to use for loop and an array to store those selected values. 

Can some one please give me code for this above task

 

 

regards,

Lucky

18 REPLIES 18

Hi Jaspal,

 

I have tried with your code. But the first value i selected is coming as undefined and later the selected values are coming with comma separated. like,

undefined,,Abraham Lincoln,Alva Pennigton,Angelo Ferentz

And the other thing is, when i select already selected user, then it is showing error message and clearing the Members field and only the field is holding undefined, like,

undefined,

 and here i want to populate with user id's not with user names like,

Abel.Tuter, Alene.Rebeck

Hi Jaspal,

 

This is the code i written

find_real_file.png

So here, what ever value i selected, the Members field is populating with user id's (as expected) but the Members field is flashing with undefined first and later in 1 second, that undefined is replacing with correct value i selected. 

One more thing is, when i select the same existing user, it is throwing error and after that it the Members field is clearing all the values and only undefined, is existing in that field. But it should not happen like that. The selected values should be there

 

 

Regards,

Lucky

Try below.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var getmembers = g_form.getValue("u_members");
    var existingList = '';
    if (getmembers != '') {
        existingList = getmembers;
    }

    var caller = g_form.getReference('u_select_the_members', getRequestedInfo);

    function getRequestedInfo(caller) {
        var newList = [];
        if (existingList.length > 0) {
            newList = existingList.split(",");
            if (newList.indexOf(caller.name) < 0) {
                newList.push(caller.name);
                g_form.setValue("u_members", newList);
            } else{
                g_form.addErrorMessage("you have already selected the user");
            }
        } else
            g_form.setValue("u_members", caller.name);
    }
    // g_form.setValue("u_members", "");
}

Hitoshi Ozawa
Giga Sage
Giga Sage

FYI:

Note: GlideRecord and g_form.getReference() are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field.

 

https://docs.servicenow.com/bundle/paris-application-development/page/script/client-scripts/concept/...