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

cwilker10
Giga Expert

that looks like general code so I can't tell you what may be wrong with it, but your approach is correct. If you alert(FieldTable.getRowCount()); you can at least see if any rows are returned which may help debug it. You may also want to alert(field1);


Hi Chris - thanks for your response. I am actually new to this and I have no idea how to set the row count to see if any rows are returned. 😞


cwilker10
Giga Expert

function onChange() {

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

if(field1 == '') return false;
alert("field1: " + field1);
var FieldTable = new GlideRecord('table_name');
FieldTable.addQuery('field_name',field1);
FieldTable.query();
alert("rowCount: " + fieldTable.getRowCount());
if(FieldTable.next()){

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

if you get this working, I would suggest NOT doing a client side GlideRecord query, but actually using AJAX. That is more advanced, however, and I understand that you need to get stuff done.


tsam
Kilo Explorer

I updated the script to include the alert and row count, but I only get the alert which shows me the sys_id of the field I am searching on....after I get an error with the script and nothing is updated.

I have attached the alert here just in case you want to see what I mean. -- Thanks.