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

tsam
Kilo Explorer

also the - 'u_target_ndm_machine' - field is referencing the machine list to select a machine name.

-Thanks.


tsam
Kilo Explorer

I updated my script to below but Iam still getting an error message when I update the target ndm machine field. I have attached the screen shots to this comment.

- Thanks.

---------------------------------------------
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.get(machine);
alert("rowCount: " + targetMachine.getRowCount());
if(targetMachine.next()){

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


tsam
Kilo Explorer

Can someone please help me with this? I tried the script suggested by cwilker10 above but i am still getting error message below:
------------------------------------------------------------------------------------------------------------------------------------------
onChange script error: [object Error]
function onChange_u_it_batch_job_documentation_u_target_ndm_machine_1(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.get(machine); alert("rowCount: " + targetMachine.getRowCount());
if(targetMachine.next()){
g_form.setValue('u_target_ndm_node',targetMachine.u_ndm_node_name);}}}


CapaJC
ServiceNow Employee
ServiceNow Employee

I don't think client-side GlideRecord has a getRowCount() method. That part is throwing a JavaScript error. Try using the following instead:



alert("rowCount: " + targetMachine.rows.length);


tsam
Kilo Explorer

Hi CapaJC -

The - alert("rowCount: " + targetMachine.rows.length); worked and I am not getting the error any longer 🙂

However, my field is still not populating with the value needed.

Thanks.