In ServiceNow, to check the values of variables (especially in the context of a Catalog Item or a Record Producer), you typically use a Client Script. Here are some methods to access and check variable values based on the specific context:
Accessing Variable Values in Client Scripts
When you are working with Order Guides or Catalog Items, you can check the values of variables directly in the Client Script. Here’s how to do it:
1. Accessing Variables Using g_form
In a Client Script, you can retrieve the values of variables using the g_form object. The key is to use the name of the variable that you want to check.
Example: Accessing a Variable
If you have a variable named sub_asset in your catalog item, you can check its value like this:
var subAssetValue = g_form.getValue('sub_asset');
2. Using g_form.getVariable (Preferred for Catalog Variables)
For catalog variables, it’s often better to use g_form.getValue() method because variables get unique names in ServiceNow.
-
Catalog Item Example: Let’s say you have a variable which is a reference variable or some specific variable you want to check against certain conditions.
-
Client Script Example:
Here's an example of a Client Script that checks a variable's value and does something with it:
function onChange(control, oldValue, newValue) {
// Assume 'sub_asset' is the variable name you're checking
var subAssetValue = g_form.getValue('sub_asset');
// Now you can check its value
if (subAssetValue === 'laptop') {
// Do something if variable value is 'laptop'
g_form.addInfoMessage('The selected sub asset is a laptop.');
} else {
// Handle other cases
g_form.addInfoMessage('The selected sub asset is not a laptop.');
}
}
3. Checking on Submit
If you want to check the value of variables when the form is submitted, you can use the onSubmit client script type:
function onSubmit() {
var shortDescriptionValue = g_form.getValue('short_description'); // Regular field
var subAssetValue = g_form.getValue('sub_asset'); // Variable
if (shortDescriptionValue == '1234' && subAssetValue == 'laptop') {
// Ensure some asset field is mandatory
g_form.setMandatory('asset_field_name', true); // Set your asset field name here
} else {
g_form.setMandatory('asset_field_name', false);
}
}
4. Use Variable Names from the Form
When you're working with catalog variables, remember that the variable names used in g_form.getValue() should exactly match the variable names configured in the catalog item.
5. Debugging
If you’re not getting the expected results, you can add some debugging information. The addInfoMessage method will help you see the values on the form in real-time:
g_form.addInfoMessage('Sub Asset Value: ' + subAssetValue);
Conclusion
You can access variable values in ServiceNow Client Scripts using g_form.getValue() or g_form.getVariable(), depending on your needs. Just ensure you are using the correct variable names as defined in your catalog item or record producer. Make sure to test your scripts in a safe environment to verify their behavior.