To achieve the functionality where an alert is shown and the value of a reference field is cleared based on a condition (in this case, if the selected server has an operational status of 1), you would typically use Client Scripts in ServiceNow.
Here’s a simplified example of how you might write such a Client Script for a Catalog Item:
Script Type: Catalog Client Script
Applies to: Catalog Item
When to run: onChange (since you want it to run every time the 'select_server' field changes)
Field name: Your reference variable name (select_server)
JavaScript Code Example:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Perform the check only if newValue is not empty
if (newValue) {
// Look up the selected server by its sys_id (newValue)
var gr = new GlideRecord('cmdb_ci_server');
gr.get(newValue); // Assuming newValue is the sys_id of the selected item
// Check if the operational status of the selected server is 1
if (gr.operational_status == '1') {
// Show an alert to the user
alert("Selected server has an operational status of 1. Please select a different server.");
// Clear the selected value
g_form.clearValue('select_server');
}
}
}
Points to Note:
- onChange: This Client Script is set to trigger on the change of the 'select_server' field.
- isLoading: This condition is used to prevent the script from running when the form is initially loaded.
- newValue: Represents the newly selected value's sys_id in the reference field. When a server is selected in the 'select_server' field, this value is the sys_id of that server record in
cmdb_ci_server.
- GlideRecord: This is used to query the
cmdb_ci_server table and check the operational status of the selected server.
- operational_status: You must ensure that the field name matches exactly, and you're comparing to the correct value (in this scenario,
1). Note that the operational status value in the condition is treated as a string ('1'); this might need to be adjusted based on the actual data type or the way the operational status field is set up in your instance.
- g_form.clearValue('select_server'): This line clears the value from the 'select_server' field if the condition is met.
Lastly, be sure to replace 'select_server' with the exact name of your reference variable if it’s different, and adjust the operational status field and value check according to your specific setup in the cmdb_ci_server table.