In servicenow, i have a catalog item and i want that if any user select any server in…
- User
In servicenow, i have a catalog item and i want that if any user select any server in the choice variable reference to cmdb_ci_server table then i need to populate the database sys_class_name in the another variable. How to do this
- Assistant
To achieve this functionality in ServiceNow, where selecting a server from a choice variable populates another variable with the database class name related to that server, you can use a combination of client scripts and possibly a catalog client script to perform server lookups. Here’s a step-by-step approach:
Step 1: Set Up Your Catalog Item
-
Create the Catalog Item: Ensure you have your catalog item ready in the Service Catalog with the necessary variables.
-
Add a Choice Variable: Add a variable of type "Choice" that references the
cmdb_ci_servertable. This will allow users to select a server. -
Add a Single Line Text Variable: Add another variable to hold the database
sys_class_name. This should be a read-only field since it will be populated automatically.
Step 2: Create the Catalog Client Script
You need to create a catalog client script that will update the database class name variable based on the server selected.
-
Navigate to Catalog Client Scripts:
- Go to the specific catalog item.
- Click on the "Catalog Client Scripts" related list.
-
Create a New Client Script:
- Click on "New" to create a new client script.
-
Configure the Client Script:
- Name: Give it a meaningful name, such as "Set Database Class Name".
- Type: Select "onChange".
- Variable name: Select the choice variable that references the
cmdb_ci_servertable. - Script:
Write the script to make an AJAX call to retrieve the
sys_class_namefrom the selected server and set it on the target variable.
Here's an example of what the client script could look like:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') { return; } // Initialize a GlideAjax object var ga = new GlideAjax('CatalogServerUtils'); ga.addParam('sysparm_name', 'getServerClassName'); ga.addParam('sysparm_server', newValue); ga.getXMLAnswer(function(response) { var className = response; g_form.setValue('database_sys_class_name', className); }); }
Step 3: Create a Script Include
You’ll need a Script Include to handle the server-side processing of fetching the
sys_class_name.-
Navigate to Script Includes:
- Go to the "Script Includes" module in ServiceNow.
-
Create a New Script Include:
- Make sure "Client callable" is checked, so it can be called from your client script.
- Write the necessary code to fetch the
sys_class_name.
Example Script Include:
var CatalogServerUtils = Class.create(); CatalogServerUtils.prototype = { initialize: function () {}, getServerClassName: function (serverSysId) { var gr = new GlideRecord('cmdb_ci_server'); gr.get(serverSysId); if (gr.isValidRecord()) { return gr.sys_class_name.toString(); } return ''; }, type: 'CatalogServerUtils' };
Key Points:
- Ensure that the variable names in your script match the exact names used in your catalog item.
- Make sure your Script Include is "Client callable" to be able to use it from the client script.
- Remember to handle any potential errors or edge cases, such as invalid server selections.
By following these steps, you should be able to dynamically update the database class name based on the server selected in your catalog item.
-