
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 03:41 AM - edited 01-30-2023 03:42 AM
Hello all,
I am tryng to write OnChange Catalog Client Script to show an alert when some values are being selected from a Lookup Select Box variable. The variable is pointing to a custom table and what I want to do is to show alert when some of the values are selected from the variable.
I have been trying to use g_form.getValue(), g_form.getOption() and g_form.getReference() methods but no success so far. The sample script is below. The alert is working as expected when value_1 is true but I need to fetch value_2 to make the script working properly. Thanks for your help in advance
var com = g_form.getValue('variable_1'); // This is working for select box variable
var loc = g_form.getValue('variable_2') ; //This is not working for lookup select box variable
if (com == 'value_1' && loc != 'value2') {
alert("ALERT");
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 05:34 AM
The two function the same way just that you most likely set both value and label of options for the 1st one the same. Thus it seems you are getting the label, but you are actually getting the value.
However in Portal it is easy to get to the label, one just needs to use
g_form.getDisplayValue('<field name>')
However you don't want to do that, you should stick with values.
E.g. someone corrects a spelling error, or renames an options and your script is hosed.
It is less likely (though not impossible) that someone will tinker with the values of options.
You just need to find out what the values or your lookup select box options are.
Also selecting a not-unique field (like Number) in filed Lookup value field is a bad advice.
You should select the same field as the one used by ServiceNow.
E.g. if the lookup table is one that is usually referenced (say sys_user or sys_user_group or cmn_country) you should select "sys_id". If the lookup table is one that contains values and those are saved to the DB, you should select value as lookup value field, like in case of sys_choice or question_choice.
There is a reason why there are fields with value and display value (like select boxes, references, list collectors, etc.): the display values are meant to be shown to the user, the values are ment to be used in scripts.
Otherwise we would just have values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 05:04 AM
your code seems working for me but I am suspecting the value returned from the Lookup Select box. make sure of the "Lookup value field" on your lookup select box variable. because it will be that value when you use the getValue() method.
please see the below screenshot
one more thing please make sure your client script's UI type is set to all. please see the screenshot below
Please mark it as correct/helpful if it helped you.
Regards,
Mohammad Amin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 05:34 AM
The two function the same way just that you most likely set both value and label of options for the 1st one the same. Thus it seems you are getting the label, but you are actually getting the value.
However in Portal it is easy to get to the label, one just needs to use
g_form.getDisplayValue('<field name>')
However you don't want to do that, you should stick with values.
E.g. someone corrects a spelling error, or renames an options and your script is hosed.
It is less likely (though not impossible) that someone will tinker with the values of options.
You just need to find out what the values or your lookup select box options are.
Also selecting a not-unique field (like Number) in filed Lookup value field is a bad advice.
You should select the same field as the one used by ServiceNow.
E.g. if the lookup table is one that is usually referenced (say sys_user or sys_user_group or cmn_country) you should select "sys_id". If the lookup table is one that contains values and those are saved to the DB, you should select value as lookup value field, like in case of sys_choice or question_choice.
There is a reason why there are fields with value and display value (like select boxes, references, list collectors, etc.): the display values are meant to be shown to the user, the values are ment to be used in scripts.
Otherwise we would just have values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2023 11:22 AM
Thanks, using getDisplayValue() method has fixed the issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 12:56 AM
You're welcome