ClientScript - onSubmit() I want to get the value of a reference field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 01:38 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 02:04 AM
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
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 02:37 AM
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()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 02:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 02:48 AM
what's the requirement here?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader