Update Reference field with a value from another Reference field?

CharlesR1
Kilo Guru

Hello all,

We need to use an onChange client script to populate a read-write reference field with the value shown on another read-only reference field. The issue is that the read-only field is itself a reference field on a different table...

We have references to the Department table (cmn_department) on both our Vendor and Purchase Order tables:

  • On the Vendor table every Vendor has a read-only entry in the Department field.
  • On the PO table (shown below) we have a reference to the Vendor table, and below this are four Vendor table fields including a read-only view of the Department field.
  • Below this, there is a read-write Department reference field on the Purchase Order table, and this now needs to be populated with the same department as shown above whenever the Vendor is changed.

find_real_file.png

The script is clearly missing something but I would be grateful if someone could point us in the right direction:

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

    if (isLoading || newValue === '') {

          return;

    }

var vendor = g_form.getValue('vendor');

var vendor1 = new GlideRecord('core_company');

vendor1.addQuery('name', vendor);

var dept //We need to add the correct script here to retrieve the department value from the vendor

if(vendor.hasNext()){

    g_form.setValue(('department') , dept);

   

  }

}

Thanks very much

Charles

1 ACCEPTED SOLUTION

Mihir Mohanta
Kilo Sage

Try this script.In place of bold text write down the department field name used in core_company table.




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


    if (isLoading || newValue === '') {


          return;


    }



var vendor = g_form.getValue('vendor');


var vendor1 = new GlideRecord('core_company');


vendor1.addQuery('sys_id', vendor);


vendor1.query();


if(vendor1.next()){



    g_form.setValue('department' , vendor1.u_department);


 


  }


}


View solution in original post

3 REPLIES 3

Dhruv Chandan
Giga Guru

Hi Russells,



After you get the department from core_company table then you may glide record to "cmn_department" table and get the object and then populate the same value using g_form.setValue.



Hoping this helps.



Regards,


Dhruv Chandan


Mihir Mohanta
Kilo Sage

Try this script.In place of bold text write down the department field name used in core_company table.




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


    if (isLoading || newValue === '') {


          return;


    }



var vendor = g_form.getValue('vendor');


var vendor1 = new GlideRecord('core_company');


vendor1.addQuery('sys_id', vendor);


vendor1.query();


if(vendor1.next()){



    g_form.setValue('department' , vendor1.u_department);


 


  }


}


...as easy as that! Thank you