in servicenow, i have a catalog item with 2 variables one is selectbox with custom choices and second is reference variable with reference qualifier simple to cmdb_ci_service table and my requirement is that based on selectbox option i need to populate the value in reference variable
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 dynamically filter a reference variable based on the selection of a select box in a ServiceNow catalog item, you can leverage client scripts. Below are the steps to implement this functionality:
Step 1: Create a Catalog Item
Ensure you have your catalog item created with the two variables:
Select Box (let’s call it u_select_box)
Reference Variable (let’s call it u_reference_variable), which references the cmdb_ci_service table.
Step 2: Create a Client Script
Navigate to your catalog item record.
Under Related Links, look for Catalog Client Scripts and click on it.
Click on New to create a new client script.
Step 3: Configure the Client Script
Set the properties of the client script as follows:
Name: Give it a meaningful name (e.g., Filter Reference Variable Based on Select Box).
Catalog Item: Choose your catalog item.
UI Type: Choose "All" or "Mobile" based on your requirement.
Script: Use the following example code:
javascript
function onLoad() {
// Clear the reference variable when the page loads
g_form.clearValue('u_reference_variable');
}
// Function to dynamically fetch options based on select box value
function onChangeSelectBox(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Define the values for the reference qualifier based on select box options
var refQual = '';
if (newValue === 'Choice1') {
refQual = 'active=true^category=Service'; // Example qualifier
} else if (newValue === 'Choice2') {
refQual = 'active=true^category=Application'; // Example qualifier
} else {
refQual = 'active=true'; // Default qualifier
}
// Set the reference qualifier for the reference variable
g_form.setReferenceQual('u_reference_variable', refQual);
}
// Use the onChange event to trigger the reference variable filter
g_form.getControl('u_select_box').onchange = onChangeSelectBox;
Explanation of the Script:
onLoad(): This function clears the reference variable when the form loads.
onChangeSelectBox(...): This function checks the selected value of the select box. Depending on the selected option, it sets a reference qualifier for the reference variable.
Replace Choice1, Choice2, and their corresponding qualifiers with your actual choices and logic.
Step 4: Test the Catalog Item
Navigate to the catalog item.
Change the select box value and observe if the reference variable is populated according to the logic you have set up.
Notes:
Ensure that the internal names of your variables match those used in the script.
Adjust your reference qualifier conditions based on your specific requirements.
Feel free to modify the script according to your requirement! If you have any questions or need further assistance, just let me know!
User
i need to do it on onchange
Assistant
Absolutely! To achieve the desired functionality based on an onChange event of the select box variable, you can utilize a client script specifically set to run when the select box value changes. Here’s a detailed guide on how to do this in ServiceNow:
Step 1: Create or Open Your Catalog Item
Make sure you have your catalog item set up with the two variables:
Select Box Variable (e.g., u_select_box).
Reference Variable that references cmdb_ci_service (e.g., u_reference_variable).
Step 2: Create a Client Script for the onChange Event
Navigate to your catalog item record.
Under the "Related Links" section, find Catalog Client Scripts and click on it.
Click on New to create a new client script.
Step 3: Configure the Client Script
Fill out the properties as follows:
Name: Choose a meaningful name (e.g., Filter Reference Variable on Select Box Change).
Catalog Item: Select your catalog item.
UI Type: Select "All".
Type: Select "onChange" from the drop-down.
Now, in the Script field, add the following code:
javascript
(function executeRule(current, previous /*null when async*/) {
// Only execute if the new value is valid
if (g_form.getValue('u_select_box') === '' || g_form.isLoading()) {
return;
}
// Get the value of the select box
var selectBoxValue = g_form.getValue('u_select_box');
// Define reference qualifier logic based on select box value
var refQual = '';
if (selectBoxValue === 'Choice1') {
refQual = 'active=true^category=Service'; // Example qualifier
} else if (selectBoxValue === 'Choice2') {
refQual = 'active=true^category=Application'; // Example qualifier
} else {
refQual = 'active=true'; // Default fallback qualifier
}
// Set the reference qualifier for the reference variable
g_form.setReferenceQual('u_reference_variable', refQual);
// Clear the reference variable when changing the select box
g_form.clearValue('u_reference_variable');
})(current, previous);
User
i need to query the cmdb_ci_service table by the name
Assistant
If you need to filter the reference variable in your ServiceNow catalog item based on the selected value of a select box and query the cmdb_ci_service table specifically by name, you can modify the onChange client script accordingly.
Steps to Implement Querying by Name
Modify the Client Script: You will update the client script that you've created to set the reference qualifier dynamically based on the select box value.
Updated Client Script
Here’s how you can achieve that. Replace the script in your onChange client script with the following:
javascript
(function() {
// Only execute if the form is loading
if (g_form.isLoading() || g_form.getValue('u_select_box') === '') {
return;
}
// Get the selected value of the select box
var selectBoxValue = g_form.getValue('u_select_box');
// Construct the reference qualifier dynamically based on the select box value
var refQual = 'name=' + selectBoxValue; // Assuming `name` is the field in cmdb_ci_service you want to match
// Set the reference qualifier for the reference variable
g_form.setReferenceQual('u_reference_variable', refQual);
// Clear the reference variable when changing the select box
g_form.clearValue('u_reference_variable');
})();
Explanation of the Updated Script:
The script starts with checking if the form is loading or if the select box has no value. If so, it exits the function early.
The selectBoxValue variable captures the currently selected value from the select box.
The refQual variable is constructed as a query to filter the cmdb_ci_service table by the name field, assuming that you want to match the name of the service with the selected value.
g_form.setReferenceQual('u_reference_variable', refQual); effectively applies this filter to the reference variable.
g_form.clearValue('u_reference_variable'); ensures that the reference variable is cleared so that the new filter can take effect immediately.
User
name is custom , if selectbox is apple then in cmdb_ci_service , i need to search name as sales and set the value
Assistant
To implement your requirement where selecting an option (like "apple") in the select box should filter the reference variable to show the entry in the cmdb_ci_service table that has a specific name (like "sales"), you can modify the client script to include a mapping of select box choices to the corresponding cmdb_ci_service.name values.
Steps to Update the Client Script
Modify the Client Script: Update the script to include mapping logic from the select box value to the corresponding cmdb_ci_service name.
Updated Client Script
Here is an updated version of your onChange client script:
javascript
(function() {
// Only execute if the form is loading or the select box is empty
if (g_form.isLoading() || g_form.getValue('u_select_box') === '') {
return;
}
// Get the currently selected value of the select box
var selectBoxValue = g_form.getValue('u_select_box');
var refQual = ''; // Reference qualifier to be set based on select box value
// Map select box values to corresponding names in cmdb_ci_service
switch(selectBoxValue) {
case 'apple':
refQual = 'name=sales'; // Maps "apple" to "sales"
break;
case 'orange':
refQual = 'name=fruit_services'; // Example: Maps "orange" to "fruit_services"
break;
case 'banana':
refQual = 'name=banana_service'; // Example: Maps "banana" to "banana_service"
break;
default:
refQual = 'active=true'; // Default condition (you can change this as needed)
break;
}
// Set the reference qualifier for the reference variable
g_form.setReferenceQual('u_reference_variable', refQual);
// Clear the reference variable when the select box changes
g_form.clearValue('u_reference_variable');
})();
User
how to do this based on script include and client script
Assistant
To achieve dynamic filtering of a reference variable based on the selection of a select box using a Script Include and a Client Script in ServiceNow, you can follow these steps:
Step 1: Create a Script Include
Navigate to Script Includes:
In your ServiceNow instance, search for "Script Includes" in the application navigator.
Create a New Script Include:
Click on the New button to create a new Script Include.
Configure the Script Include:
Name: Give it a meaningful name (e.g., ServiceNameMapper).
API Name: Automatically generated based on the name.
Active: Checked.
Client callable: Checked (this allows it to be called from client-side scripts).
Script: Enter the following code in the script field:
javascript
var ServiceNameMapper = Class.create();
ServiceNameMapper.prototype = {
initialize: function() {
},
getServiceName: function(selectBoxValue) {
var nameMapping = {
'apple': 'sales',
'orange': 'fruit_services',
'banana': 'banana_service'
// Add more mappings as needed
};
return nameMapping[selectBoxValue] || ''; // Return mapped name or empty string if no match
},
type: 'ServiceNameMapper'
};
Explanation of the Script Include:
getServiceName method: Takes the selected value from the select box and returns the corresponding service name. If no match is found, it returns an empty string.
Step 2: Create or Update the Client Script
Navigate to your catalog item.
Under the "Related Links" section, find Catalog Client Scripts and click on it.
The if condition checks if the select box has a valid value and ensures the script doesn't run when the form is loading.
The variable selectBoxValue retrieves the current value of the select box.
Depending on the selected value of the select box, it defines a reference qualifier (refQual) for the reference variable.
g_form.setReferenceQual() sets the reference variable's qualifier based on the select box choice.
g_form.clearValue('u_reference_variable') clears the reference variable when the select box changes, ensuring it reflects the new qualifier options.
Step 4: Save and Test the Client Script
Save the client script.
Navigate to the catalog item in the ServiceNow interface.
Change the value of the select box and verify that the reference variable gets populated based on your logic.
Additional Notes:
Ensure the internal names (u_select_box, u_reference_variable) in the script match those defined in your ServiceNow instance.
This script assumes a limited set of choices for the select box. You can expand the if...else condition to include as many options as necessary.
Feel free to reach out if you have any further questions or need adjustments!
Important Considerations:
Make sure the values in the select box exactly match the name field values of records in the cmdb_ci_service table for the filtering to work correctly.
If your select box contains values that do not directly correspond to cmdb_ci_service.name but rather some other underlying identifier, you'll need to either map those values in the script or adjust the code to accommodate the correct logic.
Testing:
Save the client script after modifying it.
Navigate to the respective catalog item and test it by changing the select box options.
Verify that the referenced variable only shows results that match the selected value by name from the cmdb_ci_service table.
If you have more specific requirements or need further adjustments, feel free to ask!
Explanation of the Script:
The script checks if the form is loading or the select box is empty and exits if either condition is true.
It retrieves the current value of the select box using g_form.getValue('u_select_box').
A switch statement is used to map the value of the select box to the corresponding name value in the cmdb_ci_service table.
Depending on the selected option, it creates a reference qualifier (refQual). In this case, selecting "apple" will set the qualifier to look for a cmdb_ci_service with the name "sales".
The reference qualifier is then set for the reference variable using g_form.setReferenceQual('u_reference_variable', refQual).
Finally, the reference variable is cleared to ensure it shows the filtered results based on the selection.
Testing:
Save the updated client script.
Go to the catalog item and try changing the select box value to "apple", "orange", or "banana", and observe that the reference variable now only shows options corresponding to the specified names in the cmdb_ci_service table.
Notes:
You can extend the mapping in the switch statement to include any additional options you have in your select box.
Make sure that the names you are comparing against in the cmdb_ci_service table exist, and adjust the mappings as per your naming conventions.
This solution assumes that the names in the select box will always match those used in the cmdb_ci_service table for proper filtering.
If you need further adjustments or have other questions, feel free to ask!
Create a New Client Script (or update the existing one):
Name: (e.g., Filter Reference Variable on Select Box Change).
UI Type: Select "All".
Type: Select "onChange".
Script: Use the following code in the script field:
javascript
(function() {
// Only execute if the form is loading or the select box is empty
if (g_form.isLoading() || g_form.getValue('u_select_box') === '') {
return;
}
// Get the currently selected value of the select box
var selectBoxValue = g_form.getValue('u_select_box');
// Create a GlideAjax object and call the Script Include
var ga = new GlideAjax('ServiceNameMapper');
ga.addParam('sysparm_name', 'getServiceName'); // Specify the method to call
ga.addParam('selectBoxValue', selectBoxValue); // Pass the selected box value
ga.getXMLAnswer(function(response) {
var serviceName = response; // Get the service name from response
if (serviceName) {
// Construct the reference qualifier to filter by the returned service name
var refQual = 'name=' + serviceName; // Set the qualifier for reference variable
g_form.setReferenceQual('u_reference_variable', refQual);
} else {
// If no name is returned, you can also handle that case, e.g., show all or set active
g_form.setReferenceQual('u_reference_variable', 'active=true');
}
// Clear the reference variable when changing the select box
g_form.clearValue('u_reference_variable');
});
})();
Explanation of the Client Script:
GlideAjax: It creates an instance of GlideAjax, allowing the client script to call the ServiceNameMapper Script Include.
addParam: The first parameter specifies which method to call in the Script Include (in this case, getServiceName). The second parameter sends the value of the selected box.
getXMLAnswer: This method retrieves the response from the Script Include asynchronously and uses a callback function to handle the response, which contains the corresponding service name.
It then constructs a reference qualifier based on the returned service name, sets it to the reference variable, and clears the variable to ensure it updates correctly.
Step 3: Testing
Save both the Script Include and the Client Script.
Navigate to the catalog item and test the functionality by selecting values from the select box.
The reference variable should filter correctly based on the selection made.
Additional Notes:
Ensure that your map in the Script Include matches your actual values and naming conventions in cmdb_ci_service.
Adjust error handling or defaults as required based on your application's needs.
If you need more adjustments or have additional questions, feel free to ask!