- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:15 AM
Hey!
I want to autopopulate a list collector variable in a cat item as shown below:
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:
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:
And autopopulate this with the same users:
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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:30 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:22 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:30 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 08:34 AM - edited 03-04-2025 08:35 AM
It works! Thank you! But you used GlideRecord? So it is okay to use GlideRecord in a client script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 06:55 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader