Querying reference field on a client side script

Derek10
Tera Expert

I am looking to query on a client side script once the lang2 variable is changed. As the query builds on itself it should have a end result. I have this working in workflow, but not in client side.

My first alert works, but my 2nd does not. Any ideas?

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

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

  return;

  }

  alert('test1');

  //Type appropriate comment here, and begin script below

  var mod_gr = new GlideRecord('cmdb_software_product_model');

  mod_gr.addQuery('manufacturer',variables.pub2);

  mod_gr.addQuery('name',variables.prod2);

  mod_gr.addQuery('u_choice_1',variables.lang2);

  mod_gr.query();

  if (mod_gr.next()) {

  }

  else {

  // record not found

  }

  alert('test2');

  //g_form.showErrorBox("mod_gr.software_category.getDisplayValue()");

  if ( mod_gr.software_category.getDisplayValue() == 'Managed Software List (MSL)')

  {

  //g_form.setValue('variable','value');

  g_form.setVisible('ent_cost_center', false);

  g_form.setMandatory('ent_cost_center', false);

  }

  if ( mod_gr.software_category == 'Other')

  {

  g_form.setDisplay('ent_cost_center', true);

  g_form.setMandatory('ent_cost_center', true);

  }

}

1 ACCEPTED SOLUTION

Hi Derek, i believe something like the following is what ended up working, right?



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


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


      return;


  }



  //Type appropriate comment here, and begin script below


  var mod_gr = new GlideRecord('cmdb_software_product_model');


  var manu = g_form.getValue('pub2');


  var s_name = g_form.getValue('prod2');


  var lang = g_form.getValue('lang2');



  mod_gr.addQuery('manufacturer',manu);


  mod_gr.addQuery('name',s_name);


  mod_gr.addQuery('u_choice_1',lang);


  mod_gr.query(processResponse);



  function processResponse(mod_gr){


      while (mod_gr.next()) {



            if ( mod_gr.software_category == 'Managed Software List(MSL)'){


                alert('is msl');


                g_form.setMandatory('ent_cost_center', false);


                g_form.setDisplay('ent_cost_center', false);


                //g_form.setValue('variable','value');


            }



            if ( mod_gr.software_category == 'Other'){


                alert('not msl');


                g_form.setMandatory('ent_cost_center', true);


                g_form.setDisplay('ent_cost_center', true); // I believe this line might not be needed since setting it to mandatory will automatically make the field visible.


            }


      }


  }



}


View solution in original post

4 REPLIES 4

TJW2
Mega Guru

To get your variable values client side use : g_form.getValue('variables.pub2');


// instead of variables.pub2


Thanks Terri,



I've modified my code as below, trying to use alerts to see where the error is, it looks like my if statements are not working correctly. any advice?



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


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


  return;


  }


  alert('test1');


  //Type appropriate comment here, and begin script below


  var mod_gr = new GlideRecord('cmdb_software_product_model');


  var manu = g_form.getValue('variables.pub2');


  var s_name = g_form.getValue('variables.prod2');


  var lang = g_form.getValue('variables.lang2');



  mod_gr.addQuery('manufacturer',manu);


  mod_gr.addQuery('name',s_name);


  mod_gr.addQuery('u_choice_1',lang);


  mod_gr.query();



  if (mod_gr.next()) {



  }


  else {


  // record not found


  }



  alert('test2');




  if ( mod_gr.software_category.getDisplayValue() == 'Managed Software List (MSL)')


  {


  alert('is msl');


  g_form.setVisible('ent_cost_center', false);


  g_form.setMandatory('ent_cost_center', false);


  //g_form.setValue('variable','value');


  }



  if ( mod_gr.software_category == 'Other')


  {


  alert('not msl');


  g_form.setVisible('ent_cost_center', true);


  g_form.setMandatory('ent_cost_center', true);


  }




}




The alerts is msl and is not msl are not working. Which makes me think it is not querying correctly.


Hi Derek, give a try to the following:




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


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


      return;


  }



  //Type appropriate comment here, and begin script below


  var mod_gr = new GlideRecord('cmdb_software_product_model');


  var manu = g_form.getValue('pub2');


  var s_name = g_form.getValue('prod2');


  var lang = g_form.getValue('lang2');



  mod_gr.addQuery('manufacturer',manu);


  mod_gr.addQuery('name',s_name);


  mod_gr.addQuery('u_choice_1',lang);


  mod_gr.query(processResponse);



  function processResponse(mod_gr){


      while (mod_gr.next()) {



            if ( mod_gr.software_category.getDisplayValue() == 'Managed Software List (MSL)'){


                alert('is msl');


                g_form.setDisplay('ent_cost_center', false);


                g_form.setMandatory('ent_cost_center', false);


                //g_form.setValue('variable','value');


            }



            if ( mod_gr.software_category.getDisplayValue() == 'Other'){


                alert('not msl');


                g_form.setDisplay('ent_cost_center', true); // I believe this line might not be needed since setting it to mandatory will automatically make the field visible.


                g_form.setMandatory('ent_cost_center', true);


            }


      }


  }



}


Hi Derek, i believe something like the following is what ended up working, right?



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


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


      return;


  }



  //Type appropriate comment here, and begin script below


  var mod_gr = new GlideRecord('cmdb_software_product_model');


  var manu = g_form.getValue('pub2');


  var s_name = g_form.getValue('prod2');


  var lang = g_form.getValue('lang2');



  mod_gr.addQuery('manufacturer',manu);


  mod_gr.addQuery('name',s_name);


  mod_gr.addQuery('u_choice_1',lang);


  mod_gr.query(processResponse);



  function processResponse(mod_gr){


      while (mod_gr.next()) {



            if ( mod_gr.software_category == 'Managed Software List(MSL)'){


                alert('is msl');


                g_form.setMandatory('ent_cost_center', false);


                g_form.setDisplay('ent_cost_center', false);


                //g_form.setValue('variable','value');


            }



            if ( mod_gr.software_category == 'Other'){


                alert('not msl');


                g_form.setMandatory('ent_cost_center', true);


                g_form.setDisplay('ent_cost_center', true); // I believe this line might not be needed since setting it to mandatory will automatically make the field visible.


            }


      }


  }



}