Return false not working in on submit client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 09:15 PM
Hello Community,
There is a requirement where if a catalog form passes all the validations then only the form should be submitted otherwise we have to abort the on submit. It should not submit anything.
I used return false in my client script but still user able to submit the form. Please let me know for any other alternative to abort on submit action. Below is my script i used.
Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 09:19 PM
Hi @Sajal Chatterje,
please try below client script:
function onSubmit() {
// Prevent form submission initially
g_form.submitComplete = false;
var ga = new GlideAjax('CheckDuplicateDelegate');
ga.addParam('sysparm_name', 'chkDuplicate');
ga.addParam('sysparm_user', g_form.getValue('requested_by'));
ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));
// Asynchronous call to GlideAjax
ga.getXMLAnswer(checkDuplicateDelegate);
// Return false to prevent form submission until GlideAjax response is received
return false;
function checkDuplicateDelegate(answer) {
if (answer === "true") {
g_form.addErrorMessage("There is already an existing delegation for this user and delegate with the same delegated items within the same time period.");
} else {
// Allow form submission
g_form.submitComplete = true;
g_form.submit();
}
}
}
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 10:10 PM
Hi @Sajal Chatterje,
The script you wrote looking good, but its not working as expected. Check once again where you are missing, write some info and alerts then you can get to know where you are lagging..
If my answer resolves your task, please mark this as the 'correct answer' and give hit on like.
Thanks,
Aditya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 10:34 PM
Hi @Sajal Chatterje ,
Here is the Client Script which is working, I have tested it in PDI:
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('CheckDuplicateDelegate');
ga.addParam('sysparm_name', 'chkDuplicate');
ga.addParam('sysparm_user', g_form.getValue('requested_by'));
ga.addParam('sysparm_delegate', g_form.getValue('delegate_person'));
ga.getXMLAnswer(function(answer) {
if (answer == "true") {
g_form.addErrorMessage("There is already an existing delegation for this user and delegate with the same delegated items within the same time period.");
return;
}
g_scratchpad._ajaxChecked = true;
if (typeof g_form.orderNow != 'undefined') {
g_form.orderNow();
} else {
g_form.submit(g_scratchpad._action);
}
});
return false;
}
Output:
The detailed article for better understanding the above code is linked below:
How To: Async GlideAjax in an onSubmit script
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2024 08:18 AM
Hi @Sajal Chatterje ,
Thanks for marking helpful. Please also 'Accept as solution' so that it will be helpful for the future community members.
Mark this as Helpful / Accept the Solution if this helps.