g_form.setValue() fails inside getXML()
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 12:06 PM
I'm forced to call global.AbstractAjaxProcessor to query some data from the client's onSubmit() handler. The data returns okay, but I need to set a field value with the data to submit it. setValue() isn't working inside getXML(). It sets the visible value in the field, but the original value gets sent with the submit. There are no policies set. Here's a synopsis of the code. Any help will be appreciated.
function onSubmit() {
var ga = new GlideAjax('...');
...
ga.getXML(
function (response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var stooges = JSON.parse(answer); // Okay
var payload = {
'stooge1' : stooges.moe,
'stooge2' : stooges.larry
};
var payload_string = JSON.stringify(payload); // Okay
g_form.clearValue('fieldname');
g_form.setValue('fieldname',payload_string); // Silent failure; original sent
}
);
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 12:25 PM
In addition to Shivalika's response, how is 'fieldvalue' defined? add:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 12:34 PM
The field whose value I'm setting is 'Single Line Text' in the catalog form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 04:53 PM
Hello @snowblind
The issue with your script is GlideAjax and getXML code likely stems from the fact that onSubmit() is executed synchronously, but getXML() is asynchronous. Let's break it down:
Key Issue: Asynchronous Execution of getXML()
onSubmit() executes when the form is submitted.
The ga.getXML() function is called, but it does not wait for the response before onSubmit() finishes.
The function inside getXML() is a callback that executes after the server responds, but the form submission does not wait for this to complete.
By the time g_form.setValue('fieldname', payload_string); executes inside the callback, the form has already submitted with the original value, causing a silent failure.
What Actually Happens
onSubmit() runs.
ga.getXML() sends an AJAX request.
onSubmit() returns true, allowing the form to submit before getXML() completes.
getXML() response arrives after the form is already submitted, so g_form.setValue() does not affect the submitted data.
To validate that your script is fine and returns the right value, please use "return false;" at the end to abort the submission and see if the value is set.
If the value is not returned and not set on your field, then you would need to double check your script include.
If the value is returned and set correctly and the form is not submitting, Then you can try validating if the value is returned by the time the form is about to be submitted using scratchpads.
function onSubmit() {
if (g_scratchpad._ajaxChecked) {
g_scratchpad._ajaxChecked = null;
return true;
}
g_scratchpad._action = g_form.getActionName();
g_scratchpad._ajaxChecked = false;
var ga = new GlideAjax('...');
...
ga.getXML(
function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var stooges = JSON.parse(answer); // Okay
var payload = {
'stooge1': stooges.moe,
'stooge2': stooges.larry
};
var payload_string = JSON.stringify(payload); // Okay
g_form.clearValue('fieldname');
g_form.setValue('fieldname', payload_string); // Silent failure; original sent
g_scratchpad._ajaxChecked = true;
if (typeof g_form.orderNow != 'undefined') {
g_form.orderNow();
} else {
g_form.submit(g_scratchpad._action);
}
}
);
return false;
}
If it still doesn't work, please validate your script include function if it is returning the value as expected. Do gs.info to get more logs, and check the Browser Console if there are any errors thrown when submitting the form.
I hope this helps!
If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2025 06:26 AM - edited 03-28-2025 06:28 AM
That was a great explanation. Thank you. But, unfortunately, it landed me in an infinite loop, each iteration creating a request. I assume g_form.submit() calls the onSubmit handler.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 05:58 PM
Hello @snowblind
Your issue is due to the asynchronous nature of getXML(). When you call ga.getXML(), the script continues executing and submits the form before the callback function finishes. Since the field value is updated inside the callback function, it happens after the form submission, causing the original value to be sent.
Solution:
- You need to delay the form submission until after the getXML() call completes.
- To do this, return false in onSubmit() and manually submit the form after updating the field.
Update Client Script:
function onSubmit() {
var ga = new GlideAjax('YourScriptIncludeName');
ga.addParam('sysparm_name', 'YourFunctionName');
ga.getXMLAnswer(function(response) {
var stooges = JSON.parse(response); // Ensure response is valid JSON
var payload = {
'stooge1': stooges.moe,
'stooge2': stooges.larry
};
var payload_string = JSON.stringify(payload);
g_form.setValue('fieldname', payload_string);
// Manually submit after setting value
g_form.submit();
});
// Prevent default submission
return false;
}
Note:
- getXMLAnswer() is used instead of getXML() to directly return the response value.
- return false; prevents immediate form submission.
- g_form.submit(); is called inside the callback after setting the value.
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"
Thank You
Juhi Poddar