Why won't the form submit after checking for duplicate records in Client Script/Script Include?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2024 04:50 PM
I have a script include and client script where I am attempting to detect potential duplicate records and stopping form submission for confirmation if there are any duplicate records found. However, the form won't submit even if there are no duplicates. Below are the client script and script include that are being used. What is preventing the form from submitting normally?
Client Script:
function onSubmit() {
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.");
return; // Exit without submitting
}
// 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.');
return; // Exit without submitting
}
}
// Proceed with form submission if no duplicates or user confirmed
g_form.submit(); // Trigger form submission
}
Script Include:
var DuplicateRoomCheck = Class.create();
DuplicateRoomCheck.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkDuplicate: function() {
var roomNumber = this.getParameter('sysparm_room_number');
var building = this.getParameter('sysparm_parent');
var currentSysId = this.getParameter('sysparm_sys_id');
var roomGR = new GlideRecordSecure('x_waem2_asset_locs_room');
roomGR.addQuery('room_number', roomNumber);
roomGR.addQuery('parent', building);
roomGR.addQuery('sys_id', '!=', currentSysId); // Exclude the current record
roomGR.query();
var duplicateCount = 0;
var urls = '';
while (roomGR.next()) {
duplicateCount++;
if (urls != '') {
urls += ', ';
}
urls += '<a href="' + gs.getProperty('glide.servlet.uri') + roomGR.getLink() + '" target="_blank">' + roomGR.room_number + '</a>';
}
return duplicateCount + '|' + urls;
},
type: 'DuplicateRoomCheck'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-08-2024 09:21 PM
Hi @Marcel H_ ,
it's because of the return false state in the onSubmit function.
just use getXMLWait method instead of the getXMLAnswer (this might not work in the portals alternative is using GlideRecord directly in the client script without using the callBack function).
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2024 04:16 AM
An onBefore Business Rule can be a more effective approach for enforcing duplicate record checks, depending on your requirements. With this method, you don’t need to write an onSubmit Client Script or Script Include. Instead, you can implement the duplicate check logic directly within the Business Rule. If a duplicate is found, you can use current.setAbortAction(true) to prevent the record from being created or updated, ensuring the duplicate is effectively blocked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-10-2024 05:25 AM
Hi @Marcel H_ ,
Di two thing in your code.
1. Remove this “
// Return false to stop the immediate form submission return false;
2. Use getXMLWait().

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-22-2024 02:50 PM - edited ‎11-22-2024 02:52 PM
Great thanks! I got it working, however the end users would like (if possible) to have the form submit immediately after they click Ok on the confirmation dialog. Is that possible? I've tried a few things but nothing worked as intended so I went back to my original working script.
Is it possible to have the confirmation drive form submission, or will the users always have to click Submit > Ok > Submit? Below is the current working 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
}