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

ghsrikanth
Tera Guru

Hi,



The design should change, ideally only one field should refer to Company table and other field should be a string field which holds the country information -


You can make the Company Country field as string


And in the calculated tab - put the following code


current.u_company.country



Screen Shot 2016-03-21 at 7.32.55 PM.png



in this way, you dont have to write an onChange script.


The reference field may display different fields in the list, by ref_ac_columns attribute but once selected it will always display the display value.


hence to get the country info, please make the second field as String and make it read only



Screen Shot 2016-03-21 at 7.42.27 PM.png




Mark if it is helpful or correct, feedback is appreciated


Hello Srikanth, thanks you for your reply.



This did not however work, I've put this code in my company country field in the calculated tab "current.company.country" and made it to a string, do I also have to specify the ref_ac_columns attribute?


Hi Per,



It is not onChange, you have to save the record, for the country to reflect in the field, could you please try saving the record and see the value in the record?



Thanks,


What do you mean? In which field do I have to set the value?