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

rahulpandey
Kilo Sage

Hi,

Ideally you should use list type field(similar to watchlist). However below code should help(considering user list field name is u_user_list)

Type : On change

Field : Requestor

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

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

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

}

 

 

Hi Rahul, Thanks very much for the code. I will try it and let you know. But as you mentioned, is there any field I can use as list collector on the Request form? Regards, Lucky

 

Hello Rahul,

 

I used your script but its not working. If i select Abel Tuter then at the first time, Users List field is populating with undefined,Abel Tuter and later when i select alene Rebeck, then the updated field value is undefined, Alene Rebeck which means Abel Tuter is replacing with Alene Rebeck 

and other thing is, in this line of code

 

 function getRequestedInfo(requestor)

inplace of requestor, i feel we need to add caller bcoz, it is the variable holding get Reference method in your script.

 

 

Regards,

Lucky

Hi Lucky,

the code works just fine, below is my environment code:

Requestor field name : requested_for

user list : user_list

Change the field names as per your imlementation;

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

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

    function getRequestedInfo(requestor) {
        var existingList = g_form.getValue("user_list");
        var newList = [];
        if (existingList.length > 0) {
            newList = existingList.split(",");
			if(newList.indexOf(requestor.name) < 0){
            newList.push(requestor.name);
            g_form.setValue("user_list", newList);
			}
			else
				g_form.addErrorMessage("you have already selected the user");
        } else
            g_form.setValue("user_list", requestor.name);
    }
    g_form.setValue("requested_for", "");
    //Type appropriate comment here, and begin script below

}

 

find_real_file.pngAlso, there is a field type glide list, you can refer watch list field for example.

No rahul, its not coming:

 

find_real_file.png

 

 

and my Script is:

find_real_file.png