Autopopulate list collector variable on cat item

ronro2
Tera Contributor

Hey! 

I want to autopopulate a list collector variable in a cat item as shown below: 

ronro2_0-1741104128937.png

 


Business requirement: As shown in illustration above, all the connected users should be listed in the list collector variable 'virtual_pc_users' (Användare), based on the chosen CI in first variable 'virtual_pc_name' (Datornamn). 

I tried to achieve this with the following Client Script on the cat item: 

ronro2_1-1741104307619.png

And here is the catalog client script itself:

// Detta script körs när användaren ändrar referensvariabeln 'virtual_pc_name'
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }

    // Skapa ett GlideRecord-objekt för att hämta den valda virtuella datorn
    var gr = new GlideRecord('u_cmdb_ci_pc_hardware_virtual');
    
    // Sök efter den valda virtuella datorn via referensvärdet från 'virtual_pc_name'
    if (gr.get(newValue)) {
        // Hämta fältet u_vpc_assigned_to som en lista med sys_id
        var assignedUsers = gr.u_vpc_assigned_to;

        // Om det finns några användare kopplade till den virtuella datorn
        if (assignedUsers) {
            // Dela upp listan av användare (sys_ids) och fyll i list collector
            var userIds = assignedUsers.split(',');

            // Fyll list collectorn med dessa användare
            g_form.setValue('virtual_pc_users', userIds.join(','));
        } else {
            // Om inga användare är kopplade, töm list collectorn
            g_form.setValue('virtual_pc_users', '');
        }
    } else {
        // Om ingen virtuell dator finns, töm list collectorn
        g_form.setValue('virtual_pc_users', '');
    }
}


The client script should be able to bring these multiple users stored in list field 'u_vpc_assigned_to' which is located in custom table called 'u_cmdb_ci_pc_hardware_virtual', here: 

 

ronro2_3-1741104837625.png


And autopopulate this with the same users: 

ronro2_4-1741104906419.png
What am I doing wrong? Could it be that the variable 'virtual_pc_name' is a reference to alm_hardware table? Which seems to be parent table of  u_cmdb_ci_pc_hardware_virtual? 

Thanks in advance!

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

try this with GlideRecord and callback

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

    var gr = new GlideRecord("u_cmdb_ci_pc_hardware_virtual");
    gr.addQuery("asset", newValue);
    gr.query(checkRecord);
    function checkRecord(gr) {
        if (gr.next()) {
            if (gr.u_vpc_assigned_to)
                g_form.setValue('virtual_pc_users', gr.u_vpc_assigned_to.toString());
            else
                g_form.clearValue('virtual_pc_users');
        } else {
            g_form.clearValue('virtual_pc_users');
        }
    }
}

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

@ronro2 

don't use GlideRecord in client side, please use onChange with GlideAjax

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ronro2 

try this with GlideRecord and callback

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

    var gr = new GlideRecord("u_cmdb_ci_pc_hardware_virtual");
    gr.addQuery("asset", newValue);
    gr.query(checkRecord);
    function checkRecord(gr) {
        if (gr.next()) {
            if (gr.u_vpc_assigned_to)
                g_form.setValue('virtual_pc_users', gr.u_vpc_assigned_to.toString());
            else
                g_form.clearValue('virtual_pc_users');
        } else {
            g_form.clearValue('virtual_pc_users');
        }
    }
}

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

It works! Thank you! But you used GlideRecord? So it is okay to use GlideRecord in a client script? 

@ronro2 

It's with GlideRecord callback which should be allowed

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