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

Sai Kumar B
Mega Sage
Mega Sage

@Takumi Togashi 

Instead of onSubmit() use the onChange() client script on a variable to get the value from the server

change your code as below

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

var result = g_form.getReference('variables_name_1', function(user) {
g_form.setValue( 'variable_name_2' ,  user.user_name); //Always use setValue to set the value in variable
});

 

@Sai Kumar

 

Thanks for the response.

However, I would like to use onSubmit() if possible, because I would like to use the value obtained in this process in the subsequent process of onSubmit().

Is this not possible with onSubmit()?

Amit Gujarathi
Giga Sage
Giga Sage

HI @Takumi Togashi ,
I trust you are doing great.
To retrieve a value for a field from a variable of type reference in an onSubmit catalog client script in ServiceNow, you can use the following solution:

g_form.getClientScript().setValueCallback(variables_name, function(referenceValue) {
  var user = new GlideRecord('user');
  user.addQuery('sys_id', referenceValue);
  user.query();
  
  if (user.next()) {
    var userName = user.getValue('user_name');
    
    g_form.setValue('field_name', userName);
  }
});

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Ankur Bawiskar
Tera Patron
Tera Patron

@Takumi Togashi 

what's the requirement here?

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