Changing dependent field value using Catalog client script

vidhya_mouli
Giga Sage

I have these variables on my catalog item:

vidhya_mouli_0-1702039452232.png

 

application_service is on cmdb_ci_service table.

support_group is on sys_user_group table

 

When application_service changes I want support_group to change automatically.

 

This is my catalog client script:

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

   var supportGroup = ''; 
    var cmdbCiService = new GlideRecord('cmdb_ci_service');
    cmdbCiService.addQuery('sys_id', newValue);
    cmdbCiService.query();

	g_form.addInfoMessage("OUSIDE: "+newValue);
	if (cmdbCiService.next()) {
		supportGroup = cmdbCiService.getValue('support_group');
        g_form.addInfoMessage("Support: " + supportGroup);
    }
	g_form.setValue("variables.support_group", supportGroup);
}

 

I am able to fetch the sys_id correctly but not able to set the value. How can I do this?

2 ACCEPTED SOLUTIONS

SANDEEP28
Mega Sage

@vidhya_mouli This can be done without scripting. Open the "Support group" variable and go to "Auto populate" section. 

 

In dependent question, select Application service and "Sys ID" in dot walk path field.

 

SANDEEP28_0-1702040324109.png

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

Pavankumar_1
Mega Patron

Hi @vidhya_mouli ,

You don't need any scripting for this. Use the Auto populate option which is on your support group variable.

Select the dependent question as Application service and on Dot walk path select the Support group.

Refer below screen.

Screenshot 2023-12-08 at 6.38.14 PM.png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@vidhya_mouli Ideally you should never make a GlideRecord query from the client script as it doesn't work on the Service portal. You can use a combination of a GlideAjax in client script and a script include.

 

Since you are testing this script on the backend and managed to get the sys_id. You can apply the following fix to set value on the support_group field.

 

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

   var supportGroup = ''; 
    var cmdbCiService = new GlideRecord('cmdb_ci_service');
    cmdbCiService.addQuery('sys_id', newValue);
    cmdbCiService.query();

	g_form.addInfoMessage("OUSIDE: "+newValue);
	if (cmdbCiService.next()) {
		supportGroup = cmdbCiService.getValue('support_group');
        g_form.addInfoMessage("Support: " + supportGroup);
    }
	g_form.setValue("support_group", supportGroup);
}

I tried this. But it did not work.

SANDEEP28
Mega Sage

@vidhya_mouli This can be done without scripting. Open the "Support group" variable and go to "Auto populate" section. 

 

In dependent question, select Application service and "Sys ID" in dot walk path field.

 

SANDEEP28_0-1702040324109.png

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

Pavankumar_1
Mega Patron

Hi @vidhya_mouli ,

You don't need any scripting for this. Use the Auto populate option which is on your support group variable.

Select the dependent question as Application service and on Dot walk path select the Support group.

Refer below screen.

Screenshot 2023-12-08 at 6.38.14 PM.png

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar