Prevent Duplicate record insertion in table using client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 12:29 AM
Prevent Duplicate record insertion in table using client script.
Note: I have already achieved this via BR. But I want this to be achieved via client script and script include.
Client Script(OnSubmit) :

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 12:38 AM
Hi Pratiksha,
Using GlideAjax in an onSubmit() client script may not give expected results as by the time response is received the execution has already happened.
Suggest you to change it to be an onChange logic or look for a proposed workaround on using GlideAjax in an onSubmit logic.
Also, the BR approach is ideal. Any reason for going with Client script in particular?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 12:41 AM - edited 12-29-2023 12:41 AM
Hi @Pratiksha Lang1, I have updated the addParam methods in client script as per your script include. Can you try the below client script?
function onSubmit() {
if (g_scratchpad.isFormValid) { //using g_scratchpad as Async GlideAjax does not work with Onsubmit
return true;
}
var actionNames = g_form.getActionName();
var gajax = new GlideAjax('x_amspi_smdrs_app.SMDRS_DuplicateCheckUtil');
gajax.addParam('sysparm_name', 'duplicateCheck');
gajax.addParam('sysparm_group', g_form.getValue('group'));
gajax.addParam('sysparm_site', g_form.getValue('site'));
gajax.addParam('sysparm_ent_id', g_form.getValue('enterprise_id'));
gajax.getXML(getResults);
}
function getResults(response) {
var resp = response.responseXML.documentElement.getAttribute("answer");
// alert(resp);
if (resp == 'true') {
var errorMessage = "Duplicate record exist";
var userResponse = confirm(errorMessage);
if (userResponse == true) {
g_scratchpad.isFormValid = true;
g_form.submit(actionNames);
}
} else {
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
return false;
}
Regards,
Sunil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 02:11 AM
Let's give my adjustment below a try. You can find my comments on the changes I have made.
function onSubmit() {
if (g_scratchpad.isFormValid) {
return true;
}
var actionName = g_form.getActionName(); //Correct the variable name
var gajax = new GlideAjax('x_amspi_smdrs_app.SMDRS_DuplicateCheckUtil');
gajax.addParam('sysparm_name', 'duplicateCheck');
gajax.addParam('si_group', g_form.getValue('group'));
gajax.addParam('si_site', g_form.getValue('site'));
gajax.addParam('si_entid', g_form.getValue('enterprise_id'));
gajax.getXML(getResults);
return false; //Add this line prevent submit
//Move the getResults function inside the onSubmit
//Otherwise the actionName is undefined
function getResults(response) {
var resp = response.responseXML.documentElement.getAttribute("answer");
// alert(resp);
if (resp == 'true') {
var errorMessage = "Duplicate record exist";
var userResponse = confirm(errorMessage);
if (userResponse == true) {
g_scratchpad.isFormValid = true;
g_form.submit(actionName); //Correct the variable name
}
} else {
g_scratchpad.isFormValid = true;
g_form.submit(actionName);
}
return false;
}
}
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2023 03:08 AM
Hi @Pratiksha Lang1 ,
Not sure the reason why you are choosing client script but Onsubmit and GlideAjax won't be an nice idea.
My solution will be Onchange client script or business rule will be good to implement.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar