Hide choices from multiple choice list or select box

rachelconstanti
Mega Sage

I have been tasked with hiding options in a multiple choice list. (This can be changed to select box if needed).

From what I have researched this can be accomplished with a catalog client script to do this. However, I am having issues getting this script to work. 

This is part of an Order Guide

I have a variable called employment_type (which is part of a variable set).  This is a select box and one of the choices is student.

I also have variables for Desktop (dt) and Laptop (lt) which are not part of the above variable set.  These are just variables on the order guide. 

The dt choices are optiplex_standard, precision_t3000 and precision_t7000.
The lt choices are latitude_7000_13, precision_5000, tbmacbook and tbmacbookpro15.

If student is selected and dt is selected I need to hide precision_t3000 and precision_t7000
If student is selected and lt is selected I need to hide precision_5000 and tbmacbookpro15
(These machine types are not a student offering)

I started creating an onload client script but it is not working (i figured I would start with one before adding the others) but it is not working.

I've attached what I've done - what am I doing wrong?

 

 

 

 

 

 

1 ACCEPTED SOLUTION

If you are doing this in onload, then i am not sure if employment type and computer types are selected by then. Best to do this in onChange function of your computer type field, which i assume will be selected after employment type field.

Once computer type is selected, then the below code will run and make the options hidden.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
    return;
  }
  var et = g_form.getValue("employment_type");
  alert("Entered into onchange function"+newValue+"----"+et);
  if(et == 'student' && newValue == 'dt') {
    alert("Entered 1st if");
    g_form.removeOption("dt",'precision_t3000');
    g_form.removeOption("dt",'precision_t7000');
  } else if(et =='student' && newValue == 'lt') {
     alert("Entered 2nd if");
    g_form.removeOption("lt",'precision_5000');
    g_form.removeOption("lt",'tbmacbookpro15');
  }
}

I added alert statements. So if the options are still visible and if you are getting proper alert msg, then the option names are wrong. Just verify if the options names are correct or not.

If you are not getting only 1st alert but no other alerts then cross verify the values of et and newValue that you get in 1st alerts vs the ones that we mention in the code. May be the comparison is wrong. 

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

4 REPLIES 4

asifnoor
Kilo Patron

Hi,

are you sure the field names are student and precision_t3000. I assume those are values nad not the field name. So you need to change your code like below

var et = g_form.getValue("employment_type");
var mtype = g_form.getValue("type"); //put your actual field name machine type which selects desktop or laptop
if(et =='student' && mtype == 'dt') {
  g_form.removeOption("dt",'precision_t3000');
  g_form.removeOption("dt",'precision_t7000');
} else if(et =='student' && mtype == 'lt') {
  g_form.removeOption("lt",'precision_5000');
  g_form.removeOption("lt",'tbmacbookpro15');
} 

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Thank you for your response.  Your help is much appreciated.

First, I changed all the variable types from multiple choice to select box and updated the script (the field name is computer_type which I updated in the script)  

 

 

function onLoad() {
//If employee type is student, hide dts and lts that are not a valid offering
}
var et = g_form.getValue("employment_type");
var ct = g_form.getValue("computer_type");
if(et =='student' && ct == 'dt') {
g_form.removeOption("dt",'precision_t3000');
g_form.removeOption("dt",'precision_t7000');
} else if(et =='student' && ct == 'lt') {
g_form.removeOption("lt",'precision_5000');
g_form.removeOption("lt",'tbmacbookpro15');
}

The options I want to hide if student is selected are still visible...
What else am I missing?

If you are doing this in onload, then i am not sure if employment type and computer types are selected by then. Best to do this in onChange function of your computer type field, which i assume will be selected after employment type field.

Once computer type is selected, then the below code will run and make the options hidden.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
    return;
  }
  var et = g_form.getValue("employment_type");
  alert("Entered into onchange function"+newValue+"----"+et);
  if(et == 'student' && newValue == 'dt') {
    alert("Entered 1st if");
    g_form.removeOption("dt",'precision_t3000');
    g_form.removeOption("dt",'precision_t7000');
  } else if(et =='student' && newValue == 'lt') {
     alert("Entered 2nd if");
    g_form.removeOption("lt",'precision_5000');
    g_form.removeOption("lt",'tbmacbookpro15');
  }
}

I added alert statements. So if the options are still visible and if you are getting proper alert msg, then the option names are wrong. Just verify if the options names are correct or not.

If you are not getting only 1st alert but no other alerts then cross verify the values of et and newValue that you get in 1st alerts vs the ones that we mention in the code. May be the comparison is wrong. 

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Hi Asif,

 

THANK YOU a million times over!!!!  The onChange client script you provided resolved the issue.  

Best,
Rachel