how to fill a field automatically depending on a reference field

Fdeveloper
Kilo Guru

Hi,

find_real_file.png

i have two fields the first is a reference  on a table , i should once the user select the name in the field Application (1) the second field Refernence cde (2) will be filled automatically by assignment group concerning the name

this the table :

find_real_file.png

once user select in the field application "name" the filed reference cde should be fill with the assignment group of this name("test");

1 ACCEPTED SOLUTION

On the form you can do it like below

Add an oncchange client script that runs on change of Application field

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ref = g_form.getReference('<application field name>', getAppl);
    }
function getAppl(ref) {
        
        g_form.setValue('<Reference code field name>', ref.<name of field on referenced table>);
    }
}

 

If you want to do it on the list then you have to create a Business Rule.

Condition: <reference code field > is empty

AND 

Application changes

 

Before

Update

 

Script:

 

current.<reference code field> = current.<application field>.<reference code field>;

-Anurag

 

-Anurag

View solution in original post

21 REPLIES 21

hi @Anurag Tripathi ,

when i try this background script i got empty in the script :

var gr= new GlideRecord('u_applications_bte');
gr.get('2064c0b937fabe00449c138943990e74'); //this the id of teh groupe that i must get in the field refernce cde once i select the name of the app in teh field application
gs.print(gr.u_assignement_group_b.getDisplayValue());

but i noticed one thing and i try it and i can get the name of the groupe i go to the table u_application_bte and i select one record and the i copy the sys_id of the record and i put it in background script and it works i got the name of the group

find_real_file.png

var gr= new GlideRecord('u_applications_bte');
gr.get('45278be71b9909105a26a822604bcb12');
gs.print(gr.u_assignement_group_b.getDisplayValue());

 find_real_file.png

yea so you need to get this script right, now you know what sys id its expecting. 

Work your way up to identify the gap. Script is fine, it is the value you are passing is not right it seems.

-Anurag

Jaspal Singh
Mega Patron
Mega Patron

Try below.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

  var ref = g_form.getReference('u_application_bte', getAppl);
    
function getAppl(ref) {
        
        g_form.setValue('u_groupe_d_affectation_bte', ref.<assignmengroup_field_name>);//replace assignmentgroupfield name accodingly from referenced table
    }
}

hi @Jaspal Singh ;

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

  var ref = g_form.getReference('u_application_bte', getAppl);
    }
function getAppl(ref) {
        
        g_form.setValue('u_groupe_d_affectation_bte', ref.u_assignement_group_b);
    }

find_real_file.png

the name of the field Application is u_application_bte

the name of the field Refernce Cde is u_groupe_d_affectation_bte

the name of the assignment group in the table is u_assignment_group_b

but i still getting the sys_id of the group i should get the name

That is expected.

As Reference field always return sys_id

Option 1: So, you either need to chagne the field on the form to Reference instead of String

OR

Option 2: Use Script include & Client script to get the name of the group.