Change ui reference query

johnmcgrain
Kilo Explorer

Hi All,

I have a UI reference:

                            <g:ui_reference name="company" id="company" table="core_company"/>  

I would like to be able to query for that companies location on another UI reference, e.g.

                  <g:ui_reference name="location" id="location" table="cmp_location" query="nameSTARTSWITH${company}"/>  

Does anyone have thoughts on how to achieve this?

12 REPLIES 12

marcguy
ServiceNow Employee
ServiceNow Employee

Ankur Bawiskar
Tera Patron
Tera Patron

Hi John,



I have found a way to achieve this and I have developed the script and is working fine. it is restricting the locations based on the company selection


Example:


Company A   - Location 1, Location 2


Company B - Location 3, Location 4


When you select Company A in first lookup and then click on the lookup icon for locations it will show only 2 location i.e. Location 1 and Location 2


Similar is for Company B.



Here is the script.



HTML Section



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">



  Company: <g:ui_reference name="company" id="company" table="core_company" completer="AJAXTableCompleter" query="active=true" onchange="setLocationFilter()"/>



  Location: <g:ui_reference name="locationRecords" id="locationRecords" table="cmn_location"/>


</j:jelly>



Client Script



function setLocationFilter(){



  var groupSysId = gel('company').value;


  var UserLookUp = gel('lookup.locationRecords');



  var locationArray = [];


  var gr = new GlideRecord("cmn_location");


  gr.addQuery("company", groupSysId);


  gr.query();


  while (gr.next()) {


  locationArray.push(gr.sys_id.toString());


  }



  UserLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'locationRecords', 'not', 'cmn_location', '', 'false','QUERY:active=true',           'sys_idIN" + locationArray+ "', '')");



}



Please let me know if any issue while implementing this.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Angkur,

I tried using your example and also the example you probably got your information from.

Alas none of it worked until I put the dynamic part of the query in the "" after where you put your query. After a bit of research I found out that this particular part is referred to as 'additionalQual'.

The part you put your query in is 'refQualElements'. The entire format of the function 'reflistOpen' is as follows:

function reflistOpen(target, elementName, refTableName, dependent, useQBE, refQualElements, additionalQual)

BUT... while my change did use the correct reference qualifier in my UI Page's reference fields, I cannot seem to actually select an item. I see the result list in the pop-up but when you click one of the records, the window remains stationary and only produces a console error:

Uncaught TypeError: Cannot set property 'value' of null

I haven't been able to resolve this yet. Does you or anyone else have an idea? It would be greatly appreciated.

Thanks in advance.

Hi Mike,

the approach still works

I tested in Quebec instance

find_real_file.png

Regards
Ankur

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