How to clear value of g:ui_reference box in UI Page when user select different option?

Dennis10
Giga Contributor

Hi guys,

I have the following script:

HTML:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<table>
		<tr>
			<td >Source</td>
			<td><g:ui_reference id="source" name="source" table="itfm_cost_model" completer="AJAXTableCompleter" query="active=true" onChange="setUserFilter()" /></td>
		</tr>
		<tr>
			<td>Target</td>
			<td><g:ui_reference id="target" name="target" table="itfm_cost_model" completer="AJAXTableCompleter"/></td>
		</tr>
	</table>

</j:jelly>

Client Script:

function setUserFilter() {
document.getElementById('sys_display.target').value = '';
document.getElementById('target').value = '';
	var sysIDSource = gel('source').value;
	var UserLookUp = gel('lookup.target');
	var array = [];
	var gr = new GlideRecord("itfm_cost_model");
	gr.addQuery("sys_id", "!=", sysIDSource);
	//gr.addQuery('company.name', 'ACME Germany');
	gr.query();
	while(gr.next()) {
		array.push(gr.sys_id.toString());
	}
	UserLookUp.setAttribute('onclick',"mousePositionSave(event); reflistOpen( 'target', 'not', 'itfm_cost_model', '', 'false','QUERY:active=true', 'sys_idIN" + array+ "', '')");
}

I have tried with lines 2 and 3 in Client Script but it does not work. I need when user select different option in "Source", the "Target" field is cleared.

 

Thanks!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please check this

How to clear value of g:ui_reference box in UI Page when user select different option?

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

please check this

How to clear value of g:ui_reference box in UI Page when user select different option?

Regards
Ankur

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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Initially when there is no value it won't work

this worked well for me.

It cleared Source when I changed Target.

It cleared Target when I changed Source

You should also remove the 'i' icon

So I added the extra line as well

Try this

HTML:

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 = '';
	}

	var array = [];
	var gr = new GlideRecord("sys_user");
	gr.addQuery("sys_id", "!=", sysIDSource);
	gr.addQuery('company.name', 'ACME Germany');
	gr.query();
	while(gr.next()) {
		array.push(gr.sys_id.toString());
	}

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

function clearFilter(){

	document.getElementById('sys_display.source').value = '';
	document.getElementById('source').value = '';
}

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'; // remove the i icon
	}

	var array = [];
	var gr = new GlideRecord("sys_user");
	gr.addQuery("sys_id", "!=", sysIDSource);
	gr.addQuery('company.name', 'ACME Germany');
	gr.query();
	while(gr.next()) {
		array.push(gr.sys_id.toString());
	}

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

function clearFilter(){

	document.getElementById('sys_display.source').value = '';
	document.getElementById('source').value = '';
	document.getElementById('sourceLINKreplace').style.display = 'none'; // remvoe the i icon
}

Regards
Ankur

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

Hi Ankur,

The HTML part of your example is the same as the client script but without 'i' icon removal parts, if I'm not mistaken.

What HTML code changes did you make to call the clearFilter() function?

Kind regards,

Mike

Hi,

I believe these 3 lines

document.getElementById('sys_display.source').value = '';
	document.getElementById('source').value = '';
	document.getElementById('sourceLINKreplace').style.display = 'none';

Regards
Ankur

 

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