Using Glide script to update a field

tsam
Kilo Explorer

Hi -

I am trying to use a glide record to updated a field based on the selection from another field.

I have a table that has columns:

field1 field2
chair seat
table cloth

Below is that I have and it's not working: :-((

-------------------------------------------------------------
function onChange() {

var field1 = g_form.getValue('chair');

if(field1 == '') return false;

var FieldTable = new GlideRecord('table_name');
FieldTable.addQuery('field_name',field1);
FieldTable.query();

if(FieldTable.next()){

g_form.setValue('newfield',FieldTable.field2);
}
}
-------------------------------------------------------------

16 REPLIES 16

CapaJC
ServiceNow Employee
ServiceNow Employee

Aha, you're doing a get(). That means you don't need to do a next(). Just do the following (I restructured the script a bit):



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
return; // do not run on form load

var machine = g_form.getValue('u_target_ndm_machine');
if (machine == '')
return; // no value or field not on form

var targetMachine = new GlideRecord('u_machine_list');
if (!targetMachine.get(machine))
return; // did not find one

g_form.setValue('u_target_ndm_node', targetMachine.u_ndm_node_name);
}


tsam
Kilo Explorer

Hi CapaJC -

Your script worked like a CHARM!!! Thank you, thank you, thank you. :-)))))