How to create dynamic multiple variables based on the user input on the other variable

Koyel Guha
Tera Contributor

Hi, 

I have a requirement to create multiple free text variables(vendor_details) based on the user input value in the other variable(vendor_number).

If the user put 1 in the vendor_number, it should create 1 free text variables

If the user put 4 in the vendor_number, it should create 4 free text variables

If the user put 5 in the vendor_number, it should create 5 free text variables accordingly to fill the details in it.

Can someone help me with this requirement.

 

Thanks

 

4 REPLIES 4

Juhi Poddar
Kilo Patron

Hello @Koyel Guha 

You can achieve this by pre-creating the variables (e.g., vendor_detail_1, vendor_detail_2, ..., vendor_detail_5) type string in the catalog item, and then dynamically showing or hiding them based on the user's input in the vendor_number field.

Steps:

  • Create five string variables: vendor_detail_1 to vendor_detail_5.
  • Create a Catalog UI Policy with the condition: vendor_number is 5.
  • Check On Load and Reverse if false.
  • In the UI Policy Actions, set the visibility of vendor_detail_1 to vendor_detail_5 to True.

This will show the variables only when the vendor_number is 5.

Alternatively, instead of creating multiple fields, you can use a multi-line text variable and instruct users to enter each vendor’s details on a separate line.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

 

Hi Juhi,

I have to create 15 variables. based on the number provided in the vendor number, it should populate that many variables. 

if vendor number is 1, then one variable should populate.

if vendor number is 2, then 1 & 2 variable should populate.

if vendor number is 3, then 1 & 2 & 3 variable should populate.

if vendor number is 4, then 1 & 2 & 3 & 4 variable should populate.

Like this it should populate the dynamic variables based on the number provided in the input.

 

UI policy is not working.

 

Thanks.

 

Hello @Koyel Guha 

The above requirements can be met by writing a single onChange client script.

Configuration setup:

JuhiPoddar_0-1751458901967.png

Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        // Hide all vendor fields
        for (var i = 1; i <= 15; i++) {
            g_form.setVisible('vendor' + i, false);
        }
        return;
    }
    var count = parseInt(newValue);
    // Show vendor1 to vendorN, hide the rest
    for (var i = 1; i <= 15; i++) {
        if (i <= count) {
            g_form.setVisible('vendor' + i, true);
        } else {
            g_form.setVisible('vendor' + i, false);
        }
    }
}

Result:

  • Selecting a number (e.g., 5) dynamically shows vendor1 through vendor5 and hides vendor6 to vendor15.
  • Changing the number updates the visibility in real time.

JuhiPoddar_1-1751459000210.pngJuhiPoddar_2-1751459017700.png

Note:

  • The backend names for the vendor fields are intentionally named vendor1, vendor2, ..., vendor15 to keep the logic scalable and clean.
  • This is tested in my PDI and it worked for me.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Koyel Guha 

so on the fly you want to create variables?

It is not possible.

You will have to know how many variables to be created and then show/hide based on your logic.

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