We are currently experiencing intermittent login issues on Community.  The team is actively working on a fix.

Automatically Populate a Catalog Item Variable Based on Another Variable Selection

tilekarnilesh
Giga Guru

Hi Community,

I’m working on a ServiceNow catalog item and want to dynamically populate one variable based on the selection in another variable.

Scenario:

I have a variable “select_the_requested_group” with choices:

ABC

DEF

GHI

JKL

MNO

I have another variable “copy_of_select_the_requested_group” that should automatically populate with a corresponding backend value when a group is selected.
For example:

select_the_requested_group            copy_of_select_the_requested_group 
ABC                                                                              XYZ
DEF                                                                               PQR
GHI                                                                                LMN
JKL                                                                                 STU
MNO                                                                             VWX

I want this to happen automatically, without the user having to type anything.
How to Achieve This?

1 ACCEPTED SOLUTION

Sarthak Kashyap
Mega Sage

Hi @tilekarnilesh ,

 

I tried your problem in my PDI it is working fine for me please check below solution

 

Create 2 select box type fields

select_the_requested_group and copy_of_select_the_requested_group

SarthakKashyap_0-1761297148958.png

In Configuration of these fields give the same backend name of choices like below 

This is for select_the_requested_group

SarthakKashyap_1-1761297196732.png

This is for copy_of_select_the_requested_group

SarthakKashyap_2-1761297248629.png

 

Create a catalog client script which run onChange of select_the_requested_group and add below code

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

    // If user changes the application value → set I Change = Yes
	alert("Here");
    g_form.setValue('copy_of_select_the_requested_group', newValue);

}

SarthakKashyap_3-1761297295603.png

 

Note: Here if you have 50 of choices it will work fine with just one line of code, you just need to give same backend name of your choices.

 

Result 

SarthakKashyap_4-1761297334028.png

SarthakKashyap_5-1761297354344.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

 

 

 

 

View solution in original post

8 REPLIES 8

RaghavSh
Mega Patron

This should be simple, If you only have manageable number of values you can directly do it in On change client script on "select_the_requested_group" field/variable.

if(g_form.getValue('select_the_requested_group')=='ABC')
g_form.setVaue('copy_of_select_the_requested_group' ,'XYZ');
else if(g_form.getValue('select_the_requested_group')=='DEF')
g_form.setVaue('copy_of_select_the_requested_group' ,'PQR');


Please mark the answer correct/helpful accordingly.

 

 


Raghav
MVP 2023
LinkedIn

@RaghavSh  In the future, if they plan to expand their group to 50+ how can we achieve this?

@tilekarnilesh The writing the logic in code is not a good option, I would say change your variable type to select box and define the choices/dependency in sys_choice table.

 

Refer : https://www.servicenow.com/community/developer-articles/dependent-select-box-choice-variables-on-rec... 


Please mark the answer correct/helpful accordingly.


Raghav
MVP 2023
LinkedIn

Sarthak Kashyap
Mega Sage

Hi @tilekarnilesh ,

 

I tried your problem in my PDI it is working fine for me please check below solution

 

Create 2 select box type fields

select_the_requested_group and copy_of_select_the_requested_group

SarthakKashyap_0-1761297148958.png

In Configuration of these fields give the same backend name of choices like below 

This is for select_the_requested_group

SarthakKashyap_1-1761297196732.png

This is for copy_of_select_the_requested_group

SarthakKashyap_2-1761297248629.png

 

Create a catalog client script which run onChange of select_the_requested_group and add below code

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

    // If user changes the application value → set I Change = Yes
	alert("Here");
    g_form.setValue('copy_of_select_the_requested_group', newValue);

}

SarthakKashyap_3-1761297295603.png

 

Note: Here if you have 50 of choices it will work fine with just one line of code, you just need to give same backend name of your choices.

 

Result 

SarthakKashyap_4-1761297334028.png

SarthakKashyap_5-1761297354344.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak