On submit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 07:26 AM
I have an onSubmit client script that's running on a catalog item. There are 3 fields - A, B, and C. Field A is a reference type field and B and C are single line text. The aim of the catalog client script is a) Prevent submission of the form if the catalog item has the same configuration of the referenced item. b) Allow submission if the configuration is different.
Part a) is working as expected. Part b) is running into an infinite loop. Can someone tell me why this is happening and how it can be resolved? Thanks in advance 🙂
Code added below
// this is the point where the script starts malfunctioning and starts showing alert messages in a sequence
// The alert messages have been commented out, but can someone tell me how to resolve?
If I remove the g_form.submit(Action), then the form does not get submitted. Can you guide me here?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:24 AM
Do not use g_form.submit in an onSubmit client script. Return false to cancel the form submission, otherwise don't do anything and the onSubmit function will do it's thing (submit the form). Your return false was outside of the if block. I'm not seeing the point of isCIValid. If this is adding value, put the lines back in, but in the simplest format the script looks more like this:
function onSubmit() {
var Fields = new GlideAjax("Script include name");
Fields.addParam("sysparm_name", "call script include function");
Fields.addParam("sysparm_1", p1);
Fields.addParam("sysparm_2", p2);
Fields.addParam("sysparm_3", p3);
Fields.getXMLAnswer(callBack);
function callBack(answer) {
alert("in ans" + answer);
if (answer == "false") {
g_form.addErrorMessage("You cannot do it this way");
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2024 10:55 AM
I agree with @Brad Bowman on the usage of g_form.submit in an onSubmit client script as this function triggers the onSubmit() function again, leading to an infinite recursion.
To resolve this issue, you can separate the validation logic from the form submission logic. You can perform the validation synchronously before submitting the form. Can you try this approach-
function onSubmit() {
// Perform synchronous validation first
var isValid = validateForm();
// If validation passes, submit the form
if (isValid) {
return true;
} else {
return false;
}
}
function validateForm() {
var isValid = false;
var Fields = new GlideAjax("Script include name");
Fields.addParam("sysparm_name", "call script include function");
Fields.addParam("sysparm_1", p1);
Fields.addParam("sysparm_2", p2);
Fields.addParam("sysparm_3", p3);
var answer = Fields.getXMLWait();
if (answer == "false") {
g_form.addErrorMessage("You cannot do it this way");
} else {
isValid = true;
}
return isValid;
}
Please mark my answer helpful and correct.
Regards,
Amit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2024 04:39 AM - edited ‎03-21-2024 04:39 AM
Thank you, Brad and Amit for your answers. The issue was caused by another onSubmit client script running at the same time. I managed to resolve this by combining both scripts. Thanks for the input!