Based condition should not onSubmit the form else onSubmit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
6 hours ago
Hi Team,
I have a catalog item with an onSubmit client script that enforces some conditions. Currently:
The script enters the if block and shows the error message as expected.
However, when the conditions are not matched, the form does not submit as intended.
I have written the following script, but it’s not working properly. Could you please review and help fix it?
Thank you in advance for your support!
Thank you
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
Hi @Siva82 !!
This issue happens because onSubmit runs synchronously, but GlideAjax is asynchronous.
So the form returns false before the server response arrives.
Fix:
Always return false initially
Validate in the GlideAjax callback
If valid, set a global flag and re-submit the form
Allow submission on the second run using the flag
var allowSubmit = false;
function onSubmit() {
// Allow submit after async validation
if (allowSubmit) {
return true;
}
var locationSysId = g_form.getValue('current_work_location');
var accessory = (g_form.getDisplayValue('choose_accessory') || '').toLowerCase();
var hasYubikey = accessory.indexOf('yubikey') > -1;
var ga = new GlideAjax('LocationSubmitValidator');
ga.addParam('sysparm_name', 'getLocationData');
ga.addParam('sysparm_location_id', locationSysId);
ga.getXMLAnswer(function(answer) {
var data = answer ? JSON.parse(answer) : null;
// Allow submit if no data
if (!data) {
allowSubmit = true;
g_form.submit();
return;
}
// Block condition
if (
data.name.toLowerCase().indexOf('remote') > -1 &&
data.onsiteSupport == 0 &&
data.hardwareSelfService == 0 &&
!hasYubikey
) {
g_form.addErrorMessage(
'For Remote locations without onsite or hardware self-service support, a Yubikey request is mandatory.'
);
return;
}
// Allow submit
allowSubmit = true;
g_form.submit();
});
// Stop initial submit
return false;
}Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
5 hours ago
@Mohammed8 @Sarthak Kashyap
Can you please help me on it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
I Have updated script but still not works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
Hi @Mohammed8
Thank you for response
I tried above script not works