Handling “None” Choice in Catalog Item Select Box Variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 02:55 AM - edited 03-29-2024 04:53 AM
Hello everyone,
I’m working with a catalog item that includes a select box type variable. Unfortunately, this variable does not have a “None” choice(requester dont want to include None as a choice on catalog items but they need it on Native UI). However, in the native UI of the cmdb_ci_appl table, there is an option for “None.”
My challenge is to create an onChange client script that ensures the following behavior:
- If the backend choice is “None,” the catalog item should display as blank (no selection).
- Currently, it defaults to selecting the first variable.
Any guidance or suggestions on achieving this would be greatly appreciated!
Thank you in advance for your help.
Onchange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gainst = new GlideAjax('global.testing123');
gainst.addParam('sysparm_name', 'getcatupdateform');
gainst.addParam('sysparm_catupdte', g_form.getValue('appl_name'));
gainst.getXML(catupdateResponse);
function catupdateResponse(response) {
var answer1 = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(answer1);
//var data1 = JSON.stringify(data);
// alert(data1);
if (data.sur1 == null) {
g_form.setValue('var1', '', '');
} else {
g_form.setValue('var1', '', '');
g_form.setValue('var1', data.sur1, data.sur2);
//Type appropriate comment here, and begin script below
}
}
}
var testing123 = Class.create();
testing123.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getcatupdateform: function() {
var obj = {};
var par = this.getParameter("sysparm_catupdte");
//gs.addInfoMessage("sysparm_catupdte" + par);
var gr = new GlideRecord("cmdb_ci_appl");
if (gr.get(par)) {
obj.sur1 = gr.getValue('u_string_1');
obj.sur2 = gr.getDisplayValue('u_string_1');
var data = JSON.stringify(obj);
return data;
}
},
type: 'testing123'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2024 03:11 AM
Hi @Community Alums,
Hope you are doing well.
Solution Proposed
As a solution, you can edit your catalog item variable by checking the checkbox "Include none" as True that will allow you to set the value as blank or none.
For your reference, also attaching screenshot of the option available in that catalog variable of type "Select Box".
If you find this helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks 🙂
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 02:12 AM
Hi @AakashG2703 ,
Thanks for your reply!
requester dont want to include None as a choice on catalog items but they need it on Native UI only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-30-2024 03:07 AM
@Community Alums
Slight modification in client script, Please try the below code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Check if the newValue is "None"
if (newValue == 'none') {
g_form.clearValue('var1'); // Clear the value if "None" is selected
return;
}
// If newValue is not "None", perform AJAX request to retrieve data
var gainst = new GlideAjax('global.testing123');
gainst.addParam('sysparm_name', 'getcatupdateform');
gainst.addParam('sysparm_catupdte', g_form.getValue('appl_name'));
gainst.getXML(catupdateResponse);
function catupdateResponse(response) {
var answer1 = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(answer1);
// Check if the data is null or empty
if (!data || !data.sur1) {
g_form.clearValue('var1'); // Clear the value if data is null or empty
} else {
g_form.setValue('var1', data.sur1, data.sur2);
}
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2024 08:44 PM
Hi @Maddysunil , I tried above code but It is not clearing the values..