The CreatorCon Call for Content is officially open! Get started here.

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,

Try with code suggested by bhagya with following modification,

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	var gr = new GlideRecord("sys_dictionary");
	gr.addQuery("name", "u_table");
	gr.addQuery("element", newValue);
	gr.query();
	if (gr.next()) {
		g_form.setValue(' u_field_reference',gr.sys_id);
		g_form.setValue('u_field_type',gr.internal_type);
	}
	
	
	
	
	
}

 

 

For some reason I got one step further this time without any error message. I had to change the code a bit to get the u_table. Also I do not want this script to set the field type field as the is only a dot-walk from the field u_field_reference.

The issue that I have with below now is that I do not get into the if (gr.next). This results in a correct newValue and correct sys_id displaying on screen and then a message that there are no gr.next.

I guess that the reason for failure somehow has something to do with that the field u_table is of type "table_name".

	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.getUniqueValue('u_table');
		alert('newValue changed to: ' + newValue);
		alert('u_table is: ' + g_form.getUniqueValue('u_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_field_reference',gr.sys_id);
		}
		else
		{
			alert('no gr.next');
		}
	}	

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

Thank you!

Always these simple things.