Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to set a variable in Variable Set B from Variable Set A (g_form.setValue not working)

Mmodi
Tera Contributor

 

Hi ServiceNow Community,
I have a catalog item with two variable sets:

  • Variable Set A (where my script runs)
  • Variable Set B (contains the variable I need to update)

I’m using an onChange Catalog Client Script attached to a variable in Variable Set A. I’m trying to set a variable in Variable Set B (variable_b), which is a Select Box (Reference) variable.

Example:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') return;

  g_form.setValue('variable_b', 'test'); // doesn't change anything
}

It runs without errors, but the value for variable_b doesn’t update.

Questions:

  1. Does a variable being in a different variable set affect how it’s referenced by g_form.setValue() (or should the name work globally on the item)?
  2. For a Select Box (Reference) variable, what value does g_form.setValue() expect—display value (e.g., "test") or the sys_id of the referenced record?
  3. If it requires the sys_id, what’s the recommended way to set it from an onChange script (e.g., using g_form.getReference() on another field, GlideAjax lookup, etc.)?
  4. Are there timing/loading issues where the variable in Variable Set B isn’t ready yet during the onChange of Variable Set A?

Any working examples would be appreciated.

Notes:

  • Script type: Catalog Client Script onChange
  • variable_b type: Select Box (Reference)

Thanks!

0 REPLIES 0