- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2023 02:06 PM - edited ‎07-25-2023 07:49 AM
Hi,
Here I'm trying to display the date based on the previous selection.
The data is stored in the custom table and mapped accordingly.
On-Load Catalog Client Script:
variable_1 - place - select box
variable_2 - name - select box
Based on the selection of place, I need to display the names.
table name - u_table
place field on table - location
name field on table - u_name
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var provide = g_form.getValue('name');
var ga = new GlideAjax('populateData');
ga.addParam("sysparm_name", "getName");
ga.addParam("sysparm_id", provide);
ga.getXML(getResponse);
function getResponse(response) {
var values = response.responseXML.documentElement.getAttribute('answer');
var jsonObj = JSON.parse(values);
var len = jsonObj.length;
g_form.clearOptions('name');
g_form.addOption("name", "", "-- None --");
for (i = 0; i < len; i++) {
g_form.addOption('name', jsonObj[i].sys_id, jsonObj[i].location);
}
}
}
Script Include:
var populateData = Class.create();
populateData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function() {
var res = [];
var provide = this.getParameter("sysparm_id");
var check = new GlideRecord("u_table");
check.addEncodedQuery('u_nameISNOTEMPTY');
//check.addQuery("sys_id", provide);
check.query();
while (check.next()) {
var resobj = {};
resobj.sys_id = check.sys_id.toString();
resobj.location = check.getElement('u_name').getDisplayValue();
res.push(resobj);
// res = check.u_name;
}
return JSON.stringify(res);
},
type: 'populateData'
});
With the above script it's displaying all the names.
Example: From the below table when India is selected in 'place', it should display A and B in the options for 'name' field and similarly when Canada is selected in 'place', it should display C, D and E in the options for 'name' field. Please let me know how can I achieve this
Location | Name |
India | A |
India | B |
Canada | C |
Canada | D |
Canada | E |
@Rahul Talreja @Samaksh Wani @Pavankumar_1 @Sagar Pagar @Ravi Chandra_K @Ankur Bawiskar @Manmohan K
Regards,
Priya Rao
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2023 02:45 AM
try this once
Also not sure why are you using onLoad; it should be onChange since on change of place you want options to be added dynamically
var populateData = Class.create();
populateData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function() {
var place = this.getParameter('sysparm_place');
var res = [];
var check = new GlideRecord('u_table'); //table
check.addEncodedQuery('u_nameISNOTEMPTY'); //name field on table
check.addQuery('u_location.name', place); // give correct location field name here
check.query();
while (check.next()) {
var resobj={};
resobj.u_room_name = check.u_name.toString();
res.push(resobj);
}
return JSON.stringify(res);
},
type: 'populateData'
});
onChange of place variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearOptions('name'); // give here user_id variable name
if(oldValue != newValue){
var ga = new GlideAjax('populateData');
ga.addParam('sysparm_name', "getName");
ga.addParam('sysparm_place', newValue); // give here user_id variable name
ga.getXMLAnswer(function(answer){
g_form.addOption("name", "", "-- None --");
var jsonObj = JSON.parse(answer);
var len = jsonObj.length;
for (i = 0; i < len; i++) {
g_form.addOption('name', jsonObj[i].u_name, jsonObj[i].u_name);
}
});
//Type appropriate comment here, and begin script below
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
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-25-2023 11:00 AM
Hi @Priya Rao ,
As per discussion, IG the issue is resolved.
Thanks and Regards,
Rahul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2023 02:21 AM
@Ankur Bawiskar The variable type for both the field is select box but I have not given any question choices. Even for place we will be populating it from the custom table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2023 02:25 AM
so you are adding the choices dynamically from script?
name field in that table is of type string or choice?
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-25-2023 02:34 AM
@Ankur Bawiskar Yes, name field in the table is string type
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2023 02:41 AM
why no do this?
1) have reference variable Place which refers your custom table
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-25-2023 02:57 AM
@Ankur Bawiskar No I was asked to do it through script.