Need help in UI page for 'g:ui_reference_multi' usage

Divya P
Tera Contributor

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

7 REPLIES 7

@Divya P 

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+ "', '')");

 

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Divya P
Tera Contributor

@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+ "', '')");

Tushar
Kilo Sage
Kilo Sage

 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