I'm sorry to hear that the previous code didn't work for you! Let's troubleshoot and provide a refined solution based on your requirements in ServiceNow.
Client Script
Let's simplify the client script and add some debugging to help track down any issues. It will still be an onChange client script, but we need to make sure that we target the correct variable and set the query appropriately.
- Navigate to the Catalog Item
- Create a Client Script:
- Type: onChange
- Variable name: app_name (or the actual name if different).
Here’s the revised client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return; // Do nothing if the form is loading or the new value is empty
}
// Check if the selected value is 'SalesPro'
if (newValue === 'SalesPro') {
// Set the query for the service offering field
var query = 'name=SALESPRO - SO';
// Set the filter on the service offering variable
g_form.setQuery('service_offering', query); // Use the correct variable name here
// Optionally, clear the value of the service_offering if you want to prompt the user to reselect
g_form.setValue('service_offering', ''); // Adjust this if you want to retain value
} else {
// Reset the field if Sales Hub is selected or any other value
g_form.setQuery('service_offering', ''); // Reset to default if needed
}
}
Key Adjustments:
- Be sure that
'service_offering' matches exactly the variable name used in your catalog item (variable name, not the display name).
- Use debug statements to verify what values are being processed if you want by adding
gs.info() in a Script Include method to log messages.
Script Include (if a more complex filter is needed)
You can still use the Script Include if necessary, but let's ensure the client script does the necessary logic first. If you're using a Script Include to determine the query, make sure you call it correctly:
- Create or verify your Script Include. It should look like this:
var ServiceOfferingUtils = Class.create();
ServiceOfferingUtils.prototype = {
initialize: function() {},
getFilterByAppName: function(appName) {
if (appName === 'SalesPro') {
return 'name=SALESPRO - SO';
}
// Add conditions for other app_names if needed
return ''; // Returning empty if no matches found
},
type: 'ServiceOfferingUtils'
};
Modify Client Script using Script Include:
If you decide to use the Script Include, modify your client script as such:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || !newValue) {
return; // Do nothing if the form is loading or the new value is empty
}
// Create an instance of the Script Include
var serviceOfferingUtils = new ServiceOfferingUtils();
var query = serviceOfferingUtils.getFilterByAppName(newValue); // Get the query
if (query) {
g_form.setQuery('service_offering', query); // Set the filter on the service offering variable
g_form.setValue('service_offering', ''); // Clear the value if needed
} else {
g_form.setQuery('service_offering', ''); // Reset if no filter applies
}
}
Additional Debugging
If it's still not working, consider debugging steps:
- Log Messages: Use
gs.info() to log messages in both the client script and Script Include when they execute.
- Check the console of your browser's developer tools to see if you are getting any JavaScript errors when selecting choices.
- Verify the target variable names are correct by inspecting the form.
Summary
Ensure you have the right variable names and that the logic corresponds correctly to your requirements. Test each piece (the app_name variable changes, and the service_offering responds) individually, along with logging information as needed to diagnose issues. Let me know if you run into any specific errors, and I'll help address them!