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

Shweta KHAJAPUR
Tera Guru

Hi,

I'm not getting your requirement. Try With below code,

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


As I change to below code according to your comment it will give the result as commented in the code:

else if (newValue !== oldValue)
{
	var f = g_form.getUniqueValue('u_field');
	alert('u_field: ' + f);   //this does verify that I get the sys_id into the f variable
	g_form.setValue('u_field_reference', f); //This does not work
	alert('u_field_reference: ' + g_form.getReference('u_field_reference'));  //as above row does not set the value this will give back "u_field_reference: [Object Object]"
}

Hi,

Corrected your code. Try with below code,

else if (newValue !== oldValue)
{
	var f = g_form.getUniqueValue();
	alert('u_field: ' + f);   //this does verify that I get the sys_id into the f variable
	g_form.setValue('u_field_reference', f); //This does not work
	alert('u_field_reference: ' + g_form.getReference('u_field_reference').name);  //
(g_form.getReference('u_field_reference').<field from reference table that you want to display>
}

Thank you. That solves the alert issue but it does not solve why I am not able to set the field "u_field_reference". From what I have read about setting a reference type field it should just be pushing the correct sys_id into that field with g_form.setValue().

As I have verified that I have the sys_id in "f" I don´t have any more clue on what could be wrong with the code i have.