auto populate one field based on other 3 fields

akin9
Tera Contributor

Hello Exports,

We want to auto populate based on assigned to field on asset table.

cost center ,department and location.

Requirment

1.If assigned to is empty  "cost center ,department and location" fields should be empty.

2.If not should populate values  based on the assigned to .

I have tried below onchange client script but not working pls correct me.

Table =" alm_asset"

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if (newValue == '') {
g_form.clearValue('department');
g_form.clearValue('cost_center');
g_form.clearValue('location');

return;
}
var ven = g_form.getReference('assigned_to', _AssetData);

function _AssetData(ven) {
g_form.setValue('department', ven.department);
g_form.setValue('cost_center', ven.cost_center);
g_form.setValue('location', ven.location);
}
 
}

 

 

10 REPLIES 10

sonali panda1
Kilo Sage

Hi,

 

In 1st line itself you say

   if (isLoading || newValue == '') {
      return;
   }

, so it wont clear your data.   Keep it as -

   if (isLoading ) {
      return;
   }

And I would advise to write to script include to fetch the values.

Hi @sonali panda1 @Samaksh Wani ,

I have found below mentioned onchange client script is updating the fields.

can we achieve clear value using this code ? or else pls share any sample script for script include?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '' || !g_form.getControl('location'))
        return;
   
   if (g_form.getValue('install_status') == 2 || g_form.getValue('install_status') == 6 ||
       g_form.getValue('install_status') == 9)
      return;


   
    g_form.getReference('assigned_to', setLocation);
}

function setLocation(assignee) {
    if (assignee && assignee.location != '')
        g_form.setValue('location', assignee.location);
    g_form.setValue('cost_center', assignee.cost_center);
    g_form.setValue('department', assignee.department);
    g_form.setValue('company', assignee.company);
}

Hello @akin9 

 

For Clearing Values you are checking assigned to empty, that will remain at first only.

 

You have write one onLoad() Client Script for this.

 

 

function onLoad(){

var x=g_form.getValue('assigned_to');
if(x==''){
g_form.clearValue('department');
g_form.clearValue('cost_center');
g_form.clearValue('location');
}

}

 

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Sirri
Tera Guru

Hi @akin9 ,

 

If assigned to is empty you should want clear the three fields for the you can write ui policy 

condition assigned to is empty 

and in the script you can wirte

g_form.clearValue('department');
g_form.clearValue('cost_center');
g_form.clearValue('location');

If assigned to not empty 

for that you can mention auto populat depend field based on that you can kept the conditons.

use auto populate by dot walking. Like below for three fields. And write the ui plolicy also 

Sirri_0-1700723046870.png

ui policy

Sirri_1-1700723214930.png

 

below is the ui policy script

Sirri_2-1700723373212.png

Please let us know still you have any dought

akin9
Tera Contributor

Hello @Sirri @Samaksh Wani 

Thanks for the support!

I have created a onchange client script for "alm_hardware" table.

and its working fine now.

function onChange(control, oldValue, newValue, isLoading) {
 
if (newValue == '') {
g_form.clearValue('department');
g_form.clearValue('cost_center');
g_form.clearValue('location');

return;
}
}