in servicenow catalog item, i need to update one reference field based on the choice…
- User
in servicenow catalog item, i need to update one reference field based on the choice field , so i write one script include and onChange catalog client scipt, please chek and fix this
Script Include: var ServiceDetailsProvider = Class.create(); ServiceDetailsProvider.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServiceOffering: function() { var result = {}; var cmdbCI = new GlideRecord('cmdb_ci_service'); var appValue = this.getParameter('sysparm_appName'); if (appValue == 'salesproplus') { cmdbCI.addEncodedQuery('SALESPRO+ - SO'); } cmdbCI.query(); if (cmdbCI.next()) { result = cmdbCI.getUniqueValue(); } return JSON.stringify(result); }, type: 'ServiceDetailsProvider'});
Client Script: function onChange(control, oldValue, newValue, isLoading) { // Don't run if the form is loading or if no new value is selected if (isLoading || newValue == '') { return; }
if (newValue != '') { var serviceOff = new GlideAjax('ServiceDetailsProvider'); serviceOff.addParam('sysparm_name', 'getServiceOffering'); serviceOff.addParam('sysparm_appName', 'appName'); serviceOff.getXMLAnswer(function(result) { var response = JSON.parse(result); g_form.setValue('service_offering', response.toString()); }); }}
- Assistant
Your code structure looks mostly acceptable, but there are a couple of areas to address for improvement and correction. Here’s a revised version of your Script Include and Client Script:
Script Include
Your Script Include seems largely functional, but it's important to ensure you're querying based on the correct parameter and possibly improving the query condition. Instead of just returning the unique value, it might be more appropriate to return the full object or a specific field you need (like
sys_id, or any other relevant fields).Here's the corrected version:
var ServiceDetailsProvider = Class.create(); ServiceDetailsProvider.prototype = Object.extendsObject(AbstractAjaxProcessor, { getServiceOffering: function() { var result = {}; var cmdbCI = new GlideRecord('cmdb_ci_service'); var appValue = this.getParameter('sysparm_appName'); // Ensure to modify query based on your actual requirement if (appValue === 'salesproplus') { cmdbCI.addEncodedQuery('nameLIKESALESPRO'); // Adjusted to look for a name, example query } cmdbCI.query(); if (cmdbCI.next()) { result = { sys_id: cmdbCI.getUniqueValue(), name: cmdbCI.getValue('name'), // Add other fields as necessary }; } return JSON.stringify(result); }, type: 'ServiceDetailsProvider' });Client Script
In your client script, there are minor enhancements and corrections to ensure proper execution, especially ensuring you're passing the correct appName value.
Here's the corrected client script:
function onChange(control, oldValue, newValue, isLoading) { // Don't run if the form is loading or if no new value is selected if (isLoading || newValue === '') { return; } var appName = newValue; // Assuming newValue is the app name. Change if necessary. var serviceOff = new GlideAjax('ServiceDetailsProvider'); serviceOff.addParam('sysparm_name', 'getServiceOffering'); serviceOff.addParam('sysparm_appName', appName); // Change this to pass the newValue serviceOff.getXMLAnswer(function(result) { var response = JSON.parse(result); if (response.sys_id) { g_form.setValue('service_offering', response.sys_id); } else { // Handle the case when no result is found, if necessary g_form.setValue('service_offering', ''); } }); }Summary of Changes
-
Script Include:
- Improved the query logic (you might need to adjust the addEncodedQuery clause to match your specific needs).
- Ensured that more information was returned in the JSON format.
-
Client Script:
- Set the
appNamevariable to use thenewValuedirectly, so it correctly reflects the selected choice. - Added a check to clear the reference if no valid service offering was found.
- Set the
Make sure to replace any placeholder names or conditions in the query to match your actual database schema and desired outcomes. After implementing these changes, test thoroughly in your ServiceNow instance.
-