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

function onChange() {

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

if (field1 == '') return false;

var FieldTable = new GlideRecord('table_name'); //you need to specify the table name here instead of table_name
FieldTable.addQuery('field_name', field1); //sounds like field1 is a reference which is fine if the field_name is the value as the reference will give you a sys_id. You can also try something like "field_name.name", field1 if that makes more sense.
FieldTable.query();

if (FieldTable.next()) {

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


tsam
Kilo Explorer

I have tried all possible suggestions and still can't get this to work. :-((((


Can you show your code? I can probably help you figure it out if I had your code to work with.


tsam
Kilo Explorer

Thank you for helping Chris. Here is my code below:

I have a table called Machine List which I am trying to reference and that table has 3 columns - u_name (string), u_ndm_machine ( boolean - true/false) and u_ndm_node_name (string)
----------------------------------------------

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

if (!isLoading){

var machine = g_form.getValue('u_target_ndm_machine');

if(machine == ''){ return false;
}
alert("machine: " + machine);
var targetMachine = new GlideRecord('u_machine_list');
targetMachine.addQuery('u_name',machine);
targetMachine.query();
alert("rowCount: " + targetMachine.getRowCount());
if(targetMachine.next()){

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


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

if (!isLoading){

var machine = g_form.getValue('u_target_ndm_machine');

if(machine == ''){ return false;
}
alert("machine: " + machine); //did you say that this returned a sys_id and that u_machine_list is what u_target_ndm_machine references? If so, you want to just do the uncommented pieces below
var targetMachine = new GlideRecord('u_machine_list');
targetMachine.get(machine); //this will get the record based on sys_id in the u_machine_list table.
//targetMachine.addQuery('u_name',machine);
//targetMachine.query();
alert("rowCount: " + targetMachine.getRowCount());
if(targetMachine.next()){

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