- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 09:55 PM
Hi all,
I have a requirement in catalog item.
Script Include:
var autoPopulateDetails = Class.create();
autoPopulateDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails: function() {
var value;
var sysid = this.getParameter("sysparm_sysid");
var check = new GlideRecord("u_conference_room");
check.addQuery("sys_id", sysid);
check.query();
if (check.next()) {
value = check.u_room_configuration; //field name to get
return value;
}
},
type: 'autoPopulateDetails'
});
On change catalog client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("autoPopulateDetails"); //script include
ga.addParam("sysparm_name", "getDetails"); //function
ga.addParam("sysparm_sysid", newValue); //paramter pass to server
ga.getXMLAnswer(setdetails); //callback funtion
function setdetails(response) {
var values = response.split('\n');
// g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
for (var i = 0; i < values.length; i++) {
g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
}
}
}
There are 2 question fields.
Variable 1 and Variable 2
when we chose the options in variable 1, based on the values selected in the variable 1, it displays the options for the same value stored in the respective field for variable 2. It is working as expected for one time.
But if we want to change the variable 1 value, in the variable 2 it displays the value for the selected options along with the previously selected value. I don't want this to happen? We want to display the values only based on the values selected in variable 1.
What changes has to be done in the script? Anybody please let me know.
Thanks in advance!!!
@Manmohan K @Ankur Bawiskar @Amit Gujarathi @Karan Chhabra6
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 09:57 PM
then clear the options whenever onchange runs and then add the option from script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:03 PM
Hi @2022_ServiceNow,
Try this updated scripts. just remove the comment for ckearOptions() method.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("autoPopulateDetails"); //script include
ga.addParam("sysparm_name", "getDetails"); //function
ga.addParam("sysparm_sysid", newValue); //paramter pass to server
ga.getXMLAnswer(setdetails); //callback funtion
function setdetails(response) {
var values = response.split('\n');
g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
for (var i = 0; i < values.length; i++) {
g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
}
}
}
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:17 PM - edited 07-05-2023 10:26 PM
Hi @2022_ServiceNow,
If you need None as options you can add it after clearOptions() functions.
Updated scripts:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("autoPopulateDetails"); //script include
ga.addParam("sysparm_name", "getDetails"); //function
ga.addParam("sysparm_sysid", newValue); //paramter pass to server
ga.getXMLAnswer(setdetails); //callback funtion
function setdetails(response) {
var values = response.split('\n');
g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", "", "None"); // corrected label and value sequence
for (var i = 0; i < values.length; i++) {
g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
}
}
}
Updated: corrected the label and value sequence/order in addOption() method.
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:20 PM
add none like this
g_form.addOption('please_confirm_the_seating_arrangements_for_your_meeting_in_this_room', '' , '-- None --');
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 09:57 PM
then clear the options whenever onchange runs and then add the option from script
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:08 PM - edited 07-05-2023 10:18 PM
@Ankur Bawiskar Thank you for response.
I have added clear option, it's working.
In the variable I have checked 'include none' under type specifications, but in the options, it's not displaying none instead it directly selects the first option. How can we add none option so that the user can choose the options and not directly populate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:20 PM
add none like this
g_form.addOption('please_confirm_the_seating_arrangements_for_your_meeting_in_this_room', '' , '-- None --');
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:03 PM
Hi @2022_ServiceNow,
Try this updated scripts. just remove the comment for ckearOptions() method.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("autoPopulateDetails"); //script include
ga.addParam("sysparm_name", "getDetails"); //function
ga.addParam("sysparm_sysid", newValue); //paramter pass to server
ga.getXMLAnswer(setdetails); //callback funtion
function setdetails(response) {
var values = response.split('\n');
g_form.clearOptions("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room");
for (var i = 0; i < values.length; i++) {
g_form.addOption("please_confirm_the_seating_arrangements_for_your_meeting_in_this_room", values[i], values[i]);
}
}
}
Thanks,
Sagar Pagar