Fetch data into ui page dynamically

Shiva Kumar8
Kilo Guru

Hi Community,

I'm creating a Ui page(see the attached) for consumables the issue is when I select the stock room the model field should show only the models available in that stockroom, can someone please help me how to write the code for the above condition.

Thanks in advance.

1 ACCEPTED SOLUTION

Hi,

update as this

HTML:
<td>Stockroom: </td>
<td> <g:ui_reference name="stockroom" id="stockroom" table="alm_stockroom" completer="AJAXTableCompleter" onchange="fetchModels()"/></td>
</tr>
<tr>
<td>Model: </td>
<td>
<g:ui_reference name="model" id="model" table="alm_consumable"/></td>
</tr>

Client Script:

function fetchModels(){
var stockroomValue = gel('stockroom').value;
var models = gel('lookup.model');
var modelArray = [];
var gr = new GlideRecord("alm_consumable");
gr.addQuery("stockroom", stockroomValue);
gr.query();
while (gr.next()) {
modelArray.push(gr.sys_id.toString());
}
models.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'model', 'not', 'alm_consumable', '', 'false','QUERY:active=true', 'sys_idIN" + modelArray+ "', '')");
}

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Abhijit4
Mega Sage

Hi,

I have implemented such scenario in past.

I had two reference field in UI page as shown below,

Example UI page having two reference field.

UI Page :

<table>
		<tr>
			<td >Source</td>
			<td><g:ui_reference id="source" name="source" table="sys_user" completer="AJAXTableCompleter" query="active=true" onChange="setUserFilter()" /></td>
		</tr>
		<tr>
			<td>Target</td>
			<td><g:ui_reference id="target" name="target" table="sys_user" completer="AJAXTableCompleter" onChange="clearFilter()"/></td>
		</tr>
	</table>

 

Client Script :

function setUserFilter() {

	var sysIDSource = gel('source').value;
	var UserLookUp = gel('lookup.target');

	if(gel('target').value != ''){
		document.getElementById('sys_display.target').value = '';
		document.getElementById('target').value = '';
		document.getElementById('targetLINKreplace').style.display = 'none';
	}

	//Get your stock record sys_id's through GlideAjax and store it in 'array', we have used same array object in below target field.

	UserLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'target', 'not', 'sys_user', '', 'false','QUERY:active=true', 'sys_idIN" + array+ "', '')");
}

For more details you can refer below discussion where I asked similar question and Ankur provided the solution and it worked for me.

https://community.servicenow.com/community?id=community_question&sys_id=8cfa1431db0e5c90b1b102d5ca96...

Make sure you use GlideAjax in client script instead of using GlideRecord in client script as mentioned in the discussion.

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Thanks and Regards,
Abhijit

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

Hi Ankur,

I'm using the below script but not working

HTML:
<td>Stockroom: </td>
<td> <g:ui_reference name="stockroom" id="stockroom" table="alm_stockroom" completer="AJAXTableCompleter" onchange="fetchModels()"/></td>
</tr>
<tr>
<td>Model: </td>
<td>
<g:ui_reference name="cmdb_model" id="model" table="alm_consumable" query="stockroom=storkroom"/></td>
</tr>

 

client script:

function fetchModels(){
var stockroomValue = gel('stockroom').value;
var models = gel('lookup.model');
var modelArray = [];
var gr = new GlideRecord("alm_consumable");
gr.addQuery("location", stockroomValue);
gr.query();
while (gr.next()) {
modelArray.push(gr.sys_id.toString());
}
models.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'model', 'not', 'alm_consumable', '', 'false','QUERY:active=true', 'sys_idIN" + modelArray+ "', '')");
}

 

I'm also not sure where to fetch the model, right now I'm trying to fetch from alm_consumable table

can you please help if I did anything wrong in the script

 

Hi,

update as this

HTML:
<td>Stockroom: </td>
<td> <g:ui_reference name="stockroom" id="stockroom" table="alm_stockroom" completer="AJAXTableCompleter" onchange="fetchModels()"/></td>
</tr>
<tr>
<td>Model: </td>
<td>
<g:ui_reference name="model" id="model" table="alm_consumable"/></td>
</tr>

Client Script:

function fetchModels(){
var stockroomValue = gel('stockroom').value;
var models = gel('lookup.model');
var modelArray = [];
var gr = new GlideRecord("alm_consumable");
gr.addQuery("stockroom", stockroomValue);
gr.query();
while (gr.next()) {
modelArray.push(gr.sys_id.toString());
}
models.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'model', 'not', 'alm_consumable', '', 'false','QUERY:active=true', 'sys_idIN" + modelArray+ "', '')");
}

Regards
Ankur

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