What do I need to change so that the users don't need to click Submit twice?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 03:31 PM
I have a script that is checking for duplicate records and stopping submission of the form if there is a duplicate detected, asking the user to confirm before submitting. Once confirmed the user clicks Submit again and it works.
However, because of the duplicate checking, it seems that when there isn't a duplicate the user has to click Submit twice for the submission to go through instead of just once. I've tried a few things but with no luck
Here is the current script:
// Global variable to track duplicate check completion
var isDuplicateCheckCompleted = false;
function onSubmit() {
// If the duplicate check has already completed, allow submission
if (isDuplicateCheckCompleted) {
isDuplicateCheckCompleted = false; // Reset for future submissions
return true;
}
var roomNumber = g_form.getValue('room_number').trim();
var building = g_form.getValue('parent');
// If the room number or parent field is empty, allow submission
if (roomNumber === '' || building === '') {
return true; // Allow submission since no check is needed
}
var ga = new GlideAjax('x_waem2_asset_locs.DuplicateRoomCheck');
ga.addParam('sysparm_name', 'checkDuplicate');
ga.addParam('sysparm_room_number', roomNumber);
ga.addParam('sysparm_parent', building);
ga.addParam('sysparm_sys_id', g_form.getUniqueValue()); // Exclude the current record from the search
// Prevent form submission initially and use a callback
ga.getXMLAnswer(handleDuplicateCheckResponse);
// Return false to stop the immediate form submission
return false;
}
// Callback function to handle the response from the GlideAjax call
function handleDuplicateCheckResponse(answer) {
// Ensure a valid response is received
if (!answer) {
g_form.addErrorMessage("Error checking for duplicates. Please try again or contact support.");
isDuplicateCheckCompleted = true; // Allow submission retry in case of error
g_form.submit(); // Re-trigger the submission process
return;
}
// Process the answer from the server
var [duplicateCount, urls] = answer.split('|');
duplicateCount = parseInt(duplicateCount, 10);
// If duplicates are found, show a confirmation dialog
if (duplicateCount > 0) {
var confirmSubmission = confirm("There are " + duplicateCount + " potential duplicate(s) with the same room number and building. Do you want to proceed with saving this record?");
if (!confirmSubmission) {
g_form.addErrorMessage('Submission cancelled. Please correct the room number/building and submit again.');
isDuplicateCheckCompleted = true; // Allow retry
return; // Exit without submitting
}
}
// Mark the duplicate check as completed and re-submit
isDuplicateCheckCompleted = true;
g_form.submit(); // Re-trigger the submission process
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 08:07 PM
Hello @Marcel H_
The issue arises because the onSubmit function returns false initially for the duplicate check process, regardless of whether duplicates are found. This behavior forces the user to click "Submit" twice when no duplicates exist. Here is the updated script:
// Global variable to track duplicate check completion
var isDuplicateCheckCompleted = false;
function onSubmit() {
// If the duplicate check has already completed, allow submission
if (isDuplicateCheckCompleted) {
isDuplicateCheckCompleted = false; // Reset for future submissions
return true;
}
var roomNumber = g_form.getValue('room_number').trim();
var building = g_form.getValue('parent');
// If the room number or parent field is empty, allow submission
if (roomNumber === '' || building === '') {
return true; // Allow submission since no check is needed
}
var ga = new GlideAjax('x_waem2_asset_locs.DuplicateRoomCheck');
ga.addParam('sysparm_name', 'checkDuplicate');
ga.addParam('sysparm_room_number', roomNumber);
ga.addParam('sysparm_parent', building);
ga.addParam('sysparm_sys_id', g_form.getUniqueValue()); // Exclude the current record from the search
// Use a callback and stop submission initially
ga.getXMLAnswer(function (answer) {
handleDuplicateCheckResponse(answer);
});
// Return false to stop the immediate form submission
return false;
}
// Callback function to handle the response from the GlideAjax call
function handleDuplicateCheckResponse(answer) {
// Ensure a valid response is received
if (!answer) {
g_form.addErrorMessage("Error checking for duplicates. Please try again or contact support.");
isDuplicateCheckCompleted = true; // Allow submission retry in case of error
g_form.submit(); // Re-trigger the submission process
return;
}
// Process the answer from the server
var [duplicateCount, urls] = answer.split('|');
duplicateCount = parseInt(duplicateCount, 10);
if (duplicateCount > 0) {
// Show a confirmation dialog if duplicates are found
var confirmSubmission = confirm(
"There are " + duplicateCount + " potential duplicate(s) with the same room number and building. Do you want to proceed with saving this record?"
);
if (!confirmSubmission) {
g_form.addErrorMessage('Submission cancelled. Please correct the room number/building and submit again.');
return; // Exit without submitting
}
}
// Allow immediate submission if no duplicates or confirmed
isDuplicateCheckCompleted = true;
g_form.submit(); // Proceed with form submission
}
Hope this helps!
"If you found my answer helpful, please like and mark it as the "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-30-2025 10:29 AM
Unfortunately, the script functions the same as the one I have currently. The Submit button still has to be clicked twice to submit the form when there are no duplicates found. It does correctly find duplicates, but on a return of no duplicates it just appears to sit until the user clicks Submit again.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-23-2024 01:44 AM
Hello,
This is an intriguing challenge because it highlights the subtle balance between user experience and technical implementation, especially when dealing with asynchronous workflows like duplicate checks. One interesting aspect here is the reliance on a global flag (isDuplicateCheckCompleted)—while functional, it introduces potential edge cases in scenarios like rapid clicks or form state changes during processing. A more reactive approach, such as leveraging promises or async/await (if supported), could further simplify and streamline the logic.
Another layer to explore is improving user feedback during the duplicate check. Instead of silently blocking submission while waiting for the response, adding a "Processing..." overlay or a spinner could enhance clarity and reduce user frustration. This way, the user knows the system is working, not just unresponsive.
Finally, there’s room for optimization in the duplicate-check flow itself. Could the backend return richer data or context about duplicates to enhance the user confirmation process? For example, showing a summary of conflicting records directly in the dialog could turn this from a simple blocker into a value-added feature that improves data integrity.