Need help in UI page for 'g:ui_reference_multi' usage
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-31-2023 03:13 AM
I am trying to set the dynamic 'query' for a reference field (g:ui_reference_multi).
The query is set based on the value of the other field but I can't get it to work.
HTML Script:
Initially I have set the Query as below :
<g:ui_reference_multi id="multi" name="multi" table="sys_choice" query="name=u_hiring_plan_availability_hours^element=u_month" coloumns="label" onChange="onMonthChange(e)"/>
Client Script:
I am trying to show Months based on the year selected :
function getMonths(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var mnthsArr = JSON.parse(answer);
alert(mnthsArr);
var userArray = [];
//all the members of the selected group
var gmAud = new GlideRecord('sys_choice');
gmAud.addQuery('name', 'u_hiring_plan_availability_hours');
gmAud.addQuery('element', 'u_month');
gmAud.addQuery('value', mnthsArr[0]);
gmAud.query();
while (gmAud.next()) {
if (userArray.indexOf(gmAud.sys_id.toString()) == -1)
userArray.push(gmAud.sys_id.toString());
}
alert(userArray.toString());
var YearLookUp = gel('multi'); //document.getElementById("multi") //gel('lookup.multi');
YearLookUp.setAttribute('query','sys_idSTARTSWITHb8a29bf8db65fc109f9b6f8bd3961985');
}
}
Can anyone help me to set the query in client script based on change of other field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2023 04:05 AM
can you try this?
var YearLookUp = gel('lookup.multi'); //document.getElementById("multi") //gel('lookup.multi');
YearLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'multi', 'not', 'sys_choice', '', 'false','QUERY:inactive=false','sys_idIN" + userArray+ "', '')");
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2023 04:23 AM - edited ‎08-01-2023 04:31 AM
@Ankur Bawiskar I am not using g:ui_referene ui macro since I am using g:ui_reference_multi ui macro below is not working
YearLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'multi', 'not', 'sys_choice', '', 'false','QUERY:inactive=false','sys_idIN" + userArray+ "', '')");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2023 06:14 AM
I hope this helps -
<g:ui_reference_multi id="multi" name="multi" table="sys_choice" reference="sys_choice" columns="label" onchange="onMonthChange()"/>
Client Script -
function onMonthChange() {
var answer = g_form.getValue('your_other_field'); // Replace 'your_other_field' with the actual field name that holds the value to be used for the query
var mnthsArr = JSON.parse(answer);
var userArray = [];
// Query the 'sys_choice' table on the client-side to get the sys_ids for the months based on the selected date
var gmAud = new GlideRecord('sys_choice');
gmAud.addQuery('name', 'u_hiring_plan_availability_hours');
gmAud.addQuery('element', 'u_month');
gmAud.addQuery('value', mnthsArr[0]);
gmAud.query();
while (gmAud.next()) {
if (userArray.indexOf(gmAud.sys_id.toString()) === -1)
userArray.push(gmAud.sys_id.toString());
}
// Get the reference field element
var referenceField = document.getElementById('multi');
// Set the reference attribute on the g:ui_reference_multi macro with the dynamic query
referenceField.setAttribute('reference', 'sys_choice'); // Set the table name for the reference field
referenceField.setAttribute('query', "active=true^sys_idIN" + userArray.join(',')); // Set the dynamic query based on the selected date
}
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar