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 03:48 AM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 03:52 AM
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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2025 07:26 AM
It worked, thanks a lot!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 03:42 AM - edited 06-02-2023 03:42 AM
@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.
ServiceNow Community Rising Star, Class of 2023