in catalog item i have two dropdown variables, named as food and city. my requirement is that if the value of food variable is apple and city is Delhi then hide two fields from the catalog form and unhide 1 field from the form. and if the value of food variable is mango and city is noida then hide two fields from the catalog form
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To achieve your requirement in a ServiceNow catalog form, you would typically use Catalog Client Scripts. These are scripts that run on the client side and can manipulate the form based on user input. Specifically, you'll create a Catalog Client Script of type onChange that will trigger whenever there are changes in the food or city dropdown variables.
Below is a simplified version of how you can set up the script. This example assumes the variables you want to show/hide have specific names. You'll need to replace variable_to_hide_1, variable_to_hide_2, and variable_to_unhide with the actual variable names for your form. Also, ensure that you've set the variables food and city correctly in your form design.
javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var food = g_form.getValue('food');
var city = g_form.getValue('city');
// Check if food is 'apple' and city is 'Delhi'
if(food == 'apple' && city == 'Delhi') {
// Hide two fields and unhide one
g_form.setVisible('variable_to_hide_1', false);
g_form.setVisible('variable_to_hide_2', false);
g_form.setVisible('variable_to_unhide', true);
}
// Check if food is 'mango' and city is 'Noida'
else if (food == 'mango' && city == 'Noida') {
// Hide two fields
g_form.setVisible('variable_to_hide_1', false);
g_form.setVisible('variable_to_hide_2', false);
// Assuming you want to ensure the previously unhidden field is hidden now
g_form.setVisible('variable_to_unhide', false);
}
else {
// In case you want to reset the visibility if neither condition is met
g_form.setVisible('variable_to_hide_1', true);
g_form.setVisible('variable_to_hide_2', true);
g_form.setVisible('variable_to_unhide', false);
}
}
Please follow these steps to implement your script:
Go to ServiceNow and navigate to Service Catalog > Catalog Definitions > Catalog Client Scripts.
Click New to create a new script.
Fill out the form accordingly:
Name: Enter a meaningful name for your script.
Catalog Item: Select the catalog item you're working on.
Type: Choose onChange.
Variable: Set this to run on changes in both food and city variables (you might need to create two scripts if it doesn't allow multiple triggers in one script).
Script: Copy the script provided above into this field (adjusting variable names as needed).
Remember, detailed implementation might slightly vary depending on the specific version of ServiceNow and the exact requirements of your catalog item (e.g., specific conditions or additional variables). If your setup has unique constraints or additional requirements not covered here, you may need to adjust the script accordingly.