- 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 05:32 AM
since you said u_location is reference to cmn_location and in the place you have name of location; I am querying against the name field by dot walking
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 06:32 AM
@Ankur Bawiskar I tried with it, but it's not displaying any values in the name field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2023 07:18 AM
did you debug what came in alert for answer?
is the query working fine?
We can just give you the logic but actual script you need to write as per your instance etc
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 08:02 PM
Not sure why the thread is closed by marking another response as correct?
What was the issue?
I also shared the script. You should have been able to achieve it by enhancing that
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-26-2023 08:57 PM
@Ankur Bawiskar Thank you so much. The main issue was in the other script which was evaluating the input to this script. Yes, by correcting that, your script worked. I apologize for not accepting your solution.