Alert while creating duplicate records

Community Alums
Not applicable

Hi,

Requirement:

When a new Resource plan(resource_plan) is created, with all the fields having the same values like an existing Resource plan, the system should throw an error message to the user. ("There is already an existing Resource plan with the same details, Please delete this if it is created by mistake").

This error message should come only when the Record is inserted manually, but not when the user click on 'Copy Resource plan" UI Action.

Please help me with the script to achieve this.

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Harsha,

                      Create onSubmit client script and script include as below. i have created it on custom table, you can change values as per your requirements

 

Client Script :

// for testing purpose i have created onChange Client script you can use same code in onSubmit
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (g_scratchpad.isFormValid) { //using g_scratchpad as Async GlideAjax does not work with Onsubmit
        return true;
    }

    var actionName = g_form.getActionName();
    var gajax = new GlideAjax('MyFavoritesAjax');
    gajax.addParam('sysparm_name', 'validateForm');
    gajax.addParam('sysparm_city', g_form.getValue('u_city'));
    gajax.addParam('sysparm_state', g_form.getValue('u_state1'));
    gajax.addParam('sysparm_country', g_form.getValue('u_country1'));
    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(actionName);
        }

    } else {
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
    return false;
}

 

Script Include (client callable) :

var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateForm: function() {

		gs.info("Chetan-2 inside validate form");
        // to get parameter from client sid
        var city = this.getParameter('sysparm_city');
        var state = this.getParameter('sysparm_state');
        var country = this.getParameter('sysparm_country');

        var result = "";
       
        var cityGR = new GlideRecord('u_city_info');
        cityGR.addQuery('u_city', city);
        cityGR.addQuery('u_state1', state);
        cityGR.addQuery('u_country1', country);
        cityGR.addActiveQuery();
        cityGR.query();

        if (cityGR.next()) {
            result = "true";
        }
         gs.info("Chetan-2 inside validate form Result is " + result);
        return result;
    },

    type: "MyFavoritesAjax"

});

Result :find_real_file.png

Kindly mark correct and helpful if applicable

View solution in original post

3 REPLIES 3

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Harsha,

                      Create onSubmit client script and script include as below. i have created it on custom table, you can change values as per your requirements

 

Client Script :

// for testing purpose i have created onChange Client script you can use same code in onSubmit
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (g_scratchpad.isFormValid) { //using g_scratchpad as Async GlideAjax does not work with Onsubmit
        return true;
    }

    var actionName = g_form.getActionName();
    var gajax = new GlideAjax('MyFavoritesAjax');
    gajax.addParam('sysparm_name', 'validateForm');
    gajax.addParam('sysparm_city', g_form.getValue('u_city'));
    gajax.addParam('sysparm_state', g_form.getValue('u_state1'));
    gajax.addParam('sysparm_country', g_form.getValue('u_country1'));
    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(actionName);
        }

    } else {
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }
    return false;
}

 

Script Include (client callable) :

var MyFavoritesAjax = Class.create();
MyFavoritesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    validateForm: function() {

		gs.info("Chetan-2 inside validate form");
        // to get parameter from client sid
        var city = this.getParameter('sysparm_city');
        var state = this.getParameter('sysparm_state');
        var country = this.getParameter('sysparm_country');

        var result = "";
       
        var cityGR = new GlideRecord('u_city_info');
        cityGR.addQuery('u_city', city);
        cityGR.addQuery('u_state1', state);
        cityGR.addQuery('u_country1', country);
        cityGR.addActiveQuery();
        cityGR.query();

        if (cityGR.next()) {
            result = "true";
        }
         gs.info("Chetan-2 inside validate form Result is " + result);
        return result;
    },

    type: "MyFavoritesAjax"

});

Result :find_real_file.png

Kindly mark correct and helpful if applicable

Community Alums
Not applicable

Hi Chetan, 

The code is working absolutely fine except that I need the error message, not the alert message and that too after the record got saved.

Hi Harsha,

                but if you display msg after record save. and suppose you don't want that record, then you have to delete that record. that's why the best approach is before submitting, Confirm message should populate to confirm the user's response if the user clicked on OK record will get saved if he clicks on cancel record will not get saved.