Presenting a value based on another value in another field

persahlstrom
Mega Contributor

Hello, im trying to present a value in a field based on another field.

To illustrate, I have two fields: "Company" and "Company country". When I select a company from the search-function, I want the companys country to display in the "Company Country" field.

Both fields are references refering to core_companies where a company has a country.

find_real_file.png

So far, I've been buildning a Client Script which should get the job done, but somehow it doesn't. The fields name are: "company" and "x_vvab_sales_suppo_company_country" . Anyone got any idea why it's not working?

The code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
  return;

if (newValue == '') {
  g_form.setValue('x_vvab_sales_suppo_company_country', '');
  return;
}

if (!g_form.getControl('x_vvab_sales_suppo_company_country'))
  return;

var chosedCompany = g_form.getReference('company', setCompany);
}

function setCompany(chosedCompany) {
if (chosedCompany)
  g_form.setValue('x_vvab_sales_suppo_company_country', chosedCompany.country);
}

1 ACCEPTED SOLUTION

ghsrikanth
Tera Guru

Hi Per,



A small update to the script, I tried the script in my local - remove the getControl line as now the new field is not reference it may error out


and you can keep the function outside the onChange also, it still working - I have verified and its working in my local, mostly your issue will be resolved.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading)


  return;



if (newValue == '') {


  g_form.setValue('x_vvab_sales_suppo_company_country', '');


  return;


}


 


var chosedCompany = g_form.getReference('company', setCompany);



function setCompany(chosedCompany) {


  if (chosedCompany)


  g_form.setValue('x_vvab_sales_suppo_company_country', chosedCompany.country);


  }



}



Mark if it is helpful or correct, feedback is appreciated


View solution in original post

12 REPLIES 12

Oh, now i know what you mean. Yes it saved the country if i saved it. But how do I make in react on onChange?



//Per


Then you can go for onChange script as you went before - small modification -


Put the callback function inside onChange



function onChange(control, oldValue, newValue, isLoading, isTemplate) {  


if (isLoading)  


  return;  


 


if (newValue == '') {  


  g_form.setValue('x_vvab_sales_suppo_company_country', '');  


  return;  


}  


 


if (!g_form.getControl('x_vvab_sales_suppo_company_country'))  


  return;  


 


var chosedCompany = g_form.getReference('company', setCompany);  




function setCompany(chosedCompany) {  


  if (chosedCompany)  


  g_form.setValue('x_vvab_sales_suppo_company_country', chosedCompany.country);  


  }



}  



This should help


Mihir Mohanta
Kilo Sage

Hi Per,



Is country defined in core_companies table is a reference type of field ?



Thanks,


Mihir


Hello Mihir!



Yes, countries are defined i core_companies but now I've made a new field which is a String type instead of a reference.


write


alert(chosedCompany.country);


inside setCompany function and see what it is returning.



Thanks,


Mihir