We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Set reference value in client script from field of type "Field Name"

davidgustafsson
Tera Expert

I have a custom table namned "u_anonymisation_fields" with 4 fields and a display value from one of these fields:

  1. Name: u_table; Type: Table Name
  2. Name: u_field; Type: Field Name; Dependent field: u_table
  3. Name: u_field_reference; Type: Reference; Reference to table: sys_dictionary
  4. Name: u_active; Type: True/False
  5. Name: Field type; Type: display value with the field "internal_type" dot-walked from the field "Field reference"

What the user should do in this table is to

  1. choose the table
  2. choose the field

Then u_field_type should be populated automatically to display what kind of field that has been chosen.

I am trying to do that via a client script that populates the u_field_reference field. But I can´t get my client script to add anything into u_field_reference. No matter if I am removing the setValue on line 17 or not I will never come to the setRef function. I have even tried to remove the setRef with same utterly disappointing result.

I am getting a correct sys_id in the alert for u_field on line 16.

Below is a OnChange Client script that triggers on the field u_field: 

function onChange(control, oldValue, newValue, isLoading, isTemplate)
{
	if (isLoading)
	{
		return;
	}
	
	if (newValue === '')
	{
		var clearvalue;
		g_form.setValue('u_field_reference',clearvalue);
	}
	else if (newValue !== oldValue)
	{
		var f = g_form.getUniqueValue('u_field',setRef);
		alert('u_field: ' + g_form.getUniqueValue('u_field'));
		g_form.setValue('u_field_reference', f);
		alert('u_field_reference: ' + g_form.getReference('u_field_reference'));
	}
	return;
}


function setRef(f)
{
	alert('answer setRef = ' + f);
	g_form.setValue('u_field_reference', f);
}
1 ACCEPTED SOLUTION

Hi,

If you select field which was from extended table (ex: task) then it will not enter into if. Try with below code,

if (isLoading)
	{
		return;
	}
	var clearvalue;
	if (newValue === '')
	{
		alert('newValue was empty');
		g_form.setValue('u_field_reference',clearvalue);
	}
	else if (newValue !== oldValue)
	{

		var table = g_form.getValue('u_table');
		alert('newValue changed to: ' + newValue);
		alert('u_table is: ' + table );
		var gr = new GlideRecord("sys_dictionary");
		gr.addQuery("name", table);
		gr.addQuery("element", newValue);
		gr.query();
if(gr.next){
		alert('gr is: ' + gr.sys_id);
			g_form.setValue('u_reference_field',gr.sys_id);//dont leave space while defining 'u_reference_field'
			alert(g_form.getReference('u_reference_field').internal_type);
}
		else
		{
			alert('no gr.next');
		}
	}	

View solution in original post

13 REPLIES 13

Bhagya Lakshmi
Mega Guru

Hi,

Try with this

find_real_file.png

This is client side where GlideRecord() is not supported.

Hi,

For query to database  we will use GlideRecord in client side also.

For me that throws an error that I should not use GlideRecord. when searching error message I get numerous responses that I should not use GlideRecord in wrong place, as I understood it in Client script.

Show me your script once.