Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Clear out value if field is blank not working

sigmachiuta
Kilo Guru

I am doing a lookup to a table to populate a field and this is working but when i remove the value (lines 5 and 6) the field doesnt clear to blank the field i am trying to set to blank is a date field if that maters. It doesnt seem that when the field data is removed the client script runs.

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

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

  return;

  }

  if (newValue == ''){

  g_form.setValue('u_foobar','');

  }

  var gs = new GlideRecord('u_table');

  gs.addQuery('sys_id',newValue);

  gs.query();

  if (gs.next()) {

  g_form.setValue('foobar',gs.foobar);

}

1 ACCEPTED SOLUTION

Tim Deniston
Mega Sage

Line 2 is returning if the value is blank. Make line 2 only say "if (isLoading) {"


View solution in original post

3 REPLIES 3

Tim Deniston
Mega Sage

Line 2 is returning if the value is blank. Make line 2 only say "if (isLoading) {"


Abhinay Erra
Giga Sage

On which field the change trigger is on? And I see you using u_foobar, foobar? What is the variable/field name?


Try this


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


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


  if (newValue == ''){  


  g_form.setValue('u_foobar','');  


  }  


  return;  


  }  



  var gs = new GlideRecord('u_table');  


  gs.addQuery('sys_id',newValue);  


 


 


  gs.query();  


  if (gs.next()) {  


  g_form.setValue('foobar',gs.foobar);  


 


}  


Chuck Tomasi
Tera Patron

Hi,



You most likely want an else so you're not clearing the field then looking it up again (with a newValue of null). I also changed your gs to a gr variable so my eyes didn't pop out of my head thinking you were using a server side GlideSystem object.



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


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


  return;


  }


  if (newValue == ''){


  g_form.setValue('u_foobar','');


  }   else {


    var gr = new GlideRecord('u_table');


    gr.addQuery('sys_id',newValue);


    gr.query();


    if (gr.next()) {


                    g_form.setValue('u_foobar',gr.foobar);


          }


    }


}