How to Create a Form with Repeating Fields Based on Yes/No Selection in ServiceNow?

Ashok_Bandla
Tera Expert

Hi everyone,

I need help creating a form in ServiceNow with the following requirements:

  1. Variable 1: Single-line text input
  2. Variable 2: Single-line text input
  3. Variable 3: Single-line text input
  4. Variable 4: Yes/No selection

The goal is that if Variable 4 is set to "Yes," the form should repeat Variable 1, Variable 2, and Variable 3 and 4 to allow the user to enter additional data. This repetition should continue until Variable 4 is set to "No."

How can I achieve this in ServiceNow, either through form design or scripting?

Thanks in advance for any guidance!

 

7 REPLIES 7

Shivalika
Mega Sage

Hello @Ashok_Bandla 

 

You need to create a MRVS and condition on "Add Row" button there, whose visibility you will make false if the value selected for the variables 4 is yes. 

 

Let me know if you need specific script. This cannot be done without DOM Manipulation. And I don't think there is any other way of repetitive variables set. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

 

 

Hi Shivalika,

Thanks for the details! A specific script would be really helpful for implementing the MRVS and condition on the "Add Row" button. Could you please share it?

Regards,
Ashok

Hello @Ashok_Bandla 

 

Please try below 👇 script - 

 


// Define the name of the Yes/No variable (Make this configurable)
var yesNoVariable = 'variable_4'; // Replace with actual variable name

// Get the value of the Yes/No selection
var yesNoValue = gForm.getValue(yesNoVariable);

// Check if Multi-Row Variable Set exists
var mrvsName = 'multi_row_variable_set'; // Replace with actual MRVS name
var mrvsElement = gForm.getControl(mrvsName);

if (mrvsElement) {
// Hide "Add" button if "No" is selected
if (yesNoValue === 'No') {
mrvsElement.parentElement.querySelector('.variable_set_row_add').style.display = 'none';
} else {
mrvsElement.parentElement.querySelector('.variable_set_row_add').style.display = 'block';
}
}
Let me know in case of any issues. Change the names as per your requirement. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

I wrote this i debgged  but, it is not  working. Can you look at this script


function onChange(control, oldValue, newValue, isLoading) {
    // Prevent the script from running when the form is loading or if the value is empty
    if (isLoading || newValue == '') {
        return;
    }

    // Define the fields to show/hide
    var fieldNames = [
        'patient_mrn',     // Patient MRN field name
        'case_log_id',     // Case/Log ID field name
        'case_log_date'    // Case/Log Date field name
    ];

    // Check the value of the dropdown
    if (newValue == 'Yes') {
        // If "Yes" is selected, display the fields
        for (var i = 0; i < fieldNames.length; i++) {
            g_form.setDisplay(fieldNames[i], true);  // Show fields
        }
    } else {
        // If "No" is selected, hide the fields
        for (var i = 0; i < fieldNames.length; i++) {
            g_form.setDisplay(fieldNames[i], false); // Hide fields
        }
    }
}