Prevent Duplicate record insertion in table using client script

Pratiksha Lang1
Kilo Sage

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) :

 

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('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);
}

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;
}
 
 
Script Include:
 
var SMDRS_DuplicateCheckUtil = Class.create();
SMDRS_DuplicateCheckUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    duplicateCheck: function() {

        var si_group = this.getParameter('sysparm_group');
        var si_entid = this.getParameter('sysparm_ent_id');
        var si_site = this.getParameter('sysparm_site');
        var result = "";

        var gr = new GlideRecord("x_amspi_smdrs_app_role_matrix");
        gr.addQuery("group", si_group);
        gr.addQuery("enterprise_id", si_entid);
        gr.addQuery("site", si_site);
        gr.addActiveQuery();
        gr.query();
        if (gr.next()) {
            result = "true";
        }
        return result;


    },
    type: 'SMDRS_DuplicateCheckUtil'
});

 

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

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?

SunilKumar_P
Giga Sage

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

 

Tai Vu
Kilo Patron
Kilo Patron

Hi @Pratiksha Lang1 

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

Pavankumar_1
Mega Patron

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.

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar