Fetching Value (Option) from Lookup Select Box Variable - Catalog Client Script

Alp Utku
Mega Sage

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");
}

 

 

1 ACCEPTED SOLUTION

-O-
Kilo Patron
Kilo Patron

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.

View solution in original post

4 REPLIES 4

Mohammad Amin
Mega Guru

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

MohammadAmin_0-1675083734626.png

one more thing please make sure your client script's UI type is set to all. please see the screenshot below

MohammadAmin_1-1675083809166.png

Please mark it as correct/helpful if it helped you.
Regards,

Mohammad Amin

-O-
Kilo Patron
Kilo Patron

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.

Thanks, using getDisplayValue() method has fixed the issue

You're welcome