Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

ClientScript - onSubmit() I want to get the value of a reference field.

Takumi Togashi
Tera Expert

Hi,

 

I am thinking of an onSubmit catalog client script that gets another value for that field from a variable of type reference.

I tried the following script, but could not do it.
Please let me know the solution.

 

//"variables_name" contains variables of the catalog item's reference type.

var result = g_form.getReference(variables_name, function(user) {
g_form.getValue(user.user_name);
});

 

 

Thanks.

13 REPLIES 13

@Takumi Togashi 

that's a variable which is present in script; it can't be used in any subsequent process unless it's stored in some variable.

if you store it in some variable you can use in workflow or flow later on.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I would suggest you do the following on submit script (the abstract of your requirement is to do an asynchronous submit):

function onSubmit() {
  var dependentVariable = 'my_dependent_variable';
  if (g_form.getValue(dependentVariable)) {
    // if the variable is set, the g_form.getReference has successfully returned
    return true;
  }

  var action = g_form.getActionName();
  g_form.getReference('user', function (user) {
    g_form.setValue(dependentVariable, user.user_name);
    // submit again, this time it will success because the dependentVariable is set
    g_form.submit(action);
  });

  // the getReference callback is still running, do not submit yet!
  return false;
}

 

It worked, thanks a lot!

jaheerhattiwale
Mega Sage

@Takumi Togashi The code should be updated like below

 

g_form.getReference("<variables name here>", function(user) {
var result = user.getValue('user_name');
});

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023