How can i dynamically populate a List Collector on Service Portal?

Fotios Kossyvas
Tera Contributor

In Servicenow there is this OOTB Script Include named "UserGroup" (api name "global.UserGroup"), which takes as input a UserID and returns the ID's of the groups this user is member of.

FotiosKossyvas_0-1738754873257.png

I have these variables u_utente_da_copiare (Reference to sys_user) and u_gruppi_da_copiare

(List collector to sys_user_group) in a form on a Catalog Item.

I am trying to set the u_gruppi_da_copiare value to the one returned from the above Script Include function when the field u_utente_da_copiare changes value but this Catalog Client Script i created seems not to work (it does not even show the alert box).

 

FotiosKossyvas_1-1738755401430.png

This is the code :

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

    // Reset the List Collector field
	alert('ciao');
    g_form.setValue('u_gruppi_da_copiare', '');
    var ga = new GlideAjax('global.UserGroup');
	ga.addParam('sysparm_name', 'getGroups');
    ga.addParam('sysparm_userSysID', newValue);
    
    ga.getXMLAnswer(function(response) {
        if (response) {
            var groupIDs = response.split(',');
            var filter = 'sys_idIN' + groupIDs.join(',');
            
            // Use setFilter instead of setAdvancedReferenceQual in Service Catalog
            g_form.setFilter('u_gruppi_da_copiare', filter);
        }
    });
}

Could anyone tell me what i'm doing (obviously) wrong?

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotios Kossyvas 

don't set the filter & don't use any client script.

It won't work in portal side.

Use advanced ref qualifier on list collector and show groups for the user selected like this

javascript: 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.variables.u_utente_da_copiare).getMyGroups());

In the variable attributes add this

ref_qual_elements=u_utente_da_copiare

AnkurBawiskar_0-1738757238146.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Fotios Kossyvas 

don't set the filter & don't use any client script.

It won't work in portal side.

Use advanced ref qualifier on list collector and show groups for the user selected like this

javascript: 'sys_idIN' + new global.ArrayUtil().convertArray(gs.getUser().getUserByID(current.variables.u_utente_da_copiare).getMyGroups());

In the variable attributes add this

ref_qual_elements=u_utente_da_copiare

AnkurBawiskar_0-1738757238146.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Fotios Kossyvas
Tera Contributor

Hi @Ankur Bawiskar , thank you very much, your response was correct so i marked it but i would like to ask this thing too:

all i wanted was populating the list collector field u_gruppi_da_copiare with the groups memberships of the user with id u_utente_da_copiare but my script was setting the filter instead 😅

What is the correct way to do what i really want to?

Regards

Fotios

@Fotios Kossyvas 

you should call that script include from advanced ref qualifier and pass the user

Also ensure the variable attribute is set so that the list collector picks the latest user value

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hii Ankur it was very helpful

Its working very good