Need to populate variable choices into the Patient Type field, which is part of a Multi-Row Variable

prasannasun
Tera Contributor

Hi Everyone.

 

I have created two variable sets one is single row variable set and another is multi row variable set.

 

In single row I have a variable called facility which is select box with choices like MBC, FMC, MNH

and in multi row variable set I have a field called patient types. I have to show the choices in that field based on the choices selected in Facility.

 

I have written catalog client script but that is not populating since it is multi row variable set if not using variable sets logic is fine but here I wanted to work in multirow variable set. Can some one help me with this scenario please

 

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading) return;

  var field = 'patient_type';

  // Clear existing options first
  g_form.clearOptions(field);

  // Add an explicit blank/placeholder option at the top to avoid auto-selection
  g_form.addOption(field, '', '-- None --', 0);

  if (newValue == 'BMC') {
    // BMC options
    g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
    g_form.addOption(field, 'C - INTERMEDIATE/BO', 'C - INTERMEDIATE/BO');
    g_form.addOption(field, 'D - BIRTH ROOM/LDRP', 'D - BIRTH ROOM/LDRP');
    g_form.addOption(field, 'E - NICU B', 'E - NICU B');
    g_form.addOption(field, 'F - SEMIPRIVATE', 'F - SEMIPRIVATE');
    g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
    g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');
    g_form.setDisabled(field, false);

  } else if (newValue == 'FMC') {
    // FMC options
    g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
    g_form.addOption(field, 'C - NURSERY-BORDER', 'C - NURSERY-BORDER');
    g_form.addOption(field, 'R - BIRTH ROOM/OBS', 'R - BIRTH ROOM/OBS');
    g_form.addOption(field, 'T - RECURRING', 'T - RECURRING');
    g_form.addOption(field, 'U - DAYSTAY', 'U - DAYSTAY');
    g_form.addOption(field, 'V - OBSERVATION', 'V - OBSERVATION');
    g_form.addOption(field, 'W - EMERGENCY', 'W - EMERGENCY');
    g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
    g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');
    g_form.setDisabled(field, false);

  } else if (newValue == 'WING') {
    // WING options
    g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
    g_form.addOption(field, 'C - NURSERY-BORDER', 'C - NURSERY-BORDER');
    g_form.addOption(field, 'P - TELEMETRY', 'P - TELEMETRY');
    g_form.addOption(field, 'Q - OBS HIGH RISK', 'Q - OBS HIGH RISK');
    g_form.addOption(field, 'R - MATERNITY-OBS/S', 'R - MATERNITY-OBS/S');
    g_form.addOption(field, 'T - RECURRING', 'T - RECURRING');
    g_form.addOption(field, 'U - DAY STAY', 'U - DAY STAY');
    g_form.addOption(field, 'V - OBSERVATION', 'V - OBSERVATION');
    g_form.addOption(field, 'W - EMERGENCY', 'W - EMERGENCY');
    g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
    g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');
    g_form.setDisabled(field, false);

  } else if (newValue == 'BNH') {
    // BNH options
    g_form.addOption(field, 'C - REHAB SEMIPRIVA', 'C - REHAB SEMIPRIVA');
    g_form.addOption(field, 'D - REHAB PRIVATE', 'D - REHAB PRIVATE');
    g_form.addOption(field, 'E - REHAB LOA', 'E - REHAB LOA');
    g_form.setDisabled(field, false);

  } else {
    // Not a recognized facility - keep field disabled and only the blank option
    g_form.clearOptions(field);
    g_form.addOption(field, '', '-- None --', 0);
    g_form.setDisabled(field, true);
  }

  // Always clear any previously selected value so no auto-selection occurs
  g_form.setValue(field, '');
}
5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

First off you would want this to be an onLoad Catalog Client Script that applies to the MRVS, not the Catalog Item.  This will run every time a row is added or edited in the MRVS. It should look more like this:

function onLoad() {
    var field = 'patient_type'; //mrvs variable name
    var facility = g_service_catalog.parent.getValue('facility'); //single row variable set variable name  

    // Always clear any previously selected value so no auto-selection occurs
    g_form.setValue(field, '');

    // Clear existing options first
    g_form.clearOptions(field);

    // Add an explicit blank/placeholder option at the top to avoid auto-selection
    g_form.addOption(field, '', '-- None --', 0);

    if (facility == 'BMC') {
        // BMC options
        g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
        g_form.addOption(field, 'C - INTERMEDIATE/BO', 'C - INTERMEDIATE/BO');
        g_form.addOption(field, 'D - BIRTH ROOM/LDRP', 'D - BIRTH ROOM/LDRP');
        g_form.addOption(field, 'E - NICU B', 'E - NICU B');
        g_form.addOption(field, 'F - SEMIPRIVATE', 'F - SEMIPRIVATE');
        g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
        g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');

      } else if (facility == 'FMC') {
        // FMC options
        g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
        g_form.addOption(field, 'C - NURSERY-BORDER', 'C - NURSERY-BORDER');
        g_form.addOption(field, 'R - BIRTH ROOM/OBS', 'R - BIRTH ROOM/OBS');
        g_form.addOption(field, 'T - RECURRING', 'T - RECURRING');
        g_form.addOption(field, 'U - DAYSTAY', 'U - DAYSTAY');
        g_form.addOption(field, 'V - OBSERVATION', 'V - OBSERVATION');
        g_form.addOption(field, 'W - EMERGENCY', 'W - EMERGENCY');
        g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
        g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');

      } else if (facility == 'WING') {
        // WING options
        g_form.addOption(field, 'B - NEWBORN', 'B - NEWBORN');
        g_form.addOption(field, 'C - NURSERY-BORDER', 'C - NURSERY-BORDER');
        g_form.addOption(field, 'P - TELEMETRY', 'P - TELEMETRY');
        g_form.addOption(field, 'Q - OBS HIGH RISK', 'Q - OBS HIGH RISK');
        g_form.addOption(field, 'R - MATERNITY-OBS/S', 'R - MATERNITY-OBS/S');
        g_form.addOption(field, 'T - RECURRING', 'T - RECURRING');
        g_form.addOption(field, 'U - DAY STAY', 'U - DAY STAY');
        g_form.addOption(field, 'V - OBSERVATION', 'V - OBSERVATION');
        g_form.addOption(field, 'W - EMERGENCY', 'W - EMERGENCY');
        g_form.addOption(field, 'Y - ONE TIME', 'Y - ONE TIME');
       g_form.addOption(field, 'Z - BRL REGISTRATION', 'Z - BRL REGISTRATION');

    } else if (facility == 'BNH') {
        // BNH options
        g_form.addOption(field, 'C - REHAB SEMIPRIVA', 'C - REHAB SEMIPRIVA');
        g_form.addOption(field, 'D - REHAB PRIVATE', 'D - REHAB PRIVATE');
        g_form.addOption(field, 'E - REHAB LOA', 'E - REHAB LOA');

    } else {
        // Not a recognized facility - keep field disabled and only the blank option
        g_form.setReadOnly(field, true);
    }
}

depending on your variable names, and the intention behind the 'setDisabled' attempt.  If you mean setReadOnly you don't need that to be false in every condition if it is not read-only by default when the MRVS loads. You've cleared the options and added the blank overall, so you don't need to do so again in the else block, especially if the variable will be read-only - it doesn't matter how many choices there are if no one can see them.  And I would clear the variable value first before clearing then adding options...

 

Hi  Brad,

 

Thanks a lot for response. I have created the onload script in variable set( MRVS) level onload and updated the script similar to the one you have provided but it is not populatating patient types once I select facility value.

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@prasannasun 

you will require onLoad client script which Applies to MRVS

Approach shared by @Brad Bowman  will work for sure.

syntax to get outside variable in MRVS client script is this

var variableValue = g_service_catalog.parent.getValue("outsideVariableName");

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@prasannasun 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader