error field message is getting hidden within 1 sec in record producer onchange script

Amit Dey1
Tera Contributor

I made the below code to show error field message in record producer, when the user add duplicate name then we have to show them the error field message, but sometimes the error message is getting hidden with in 1 sec,

code-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    g_form.hideFieldMsg('RP_application_name');
    var length = g_form.getValue("RP_application_name").length;
    if (length <= 50 && newValue != '') {
        g_form.hideFieldMsg('RP_application_name');
        var ga = new GlideAjax("utils");
        ga.addParam("sysparm_name", "checkIfApplicationNameExists");
        ga.addParam("sysparm_appName", newValue);
        ga.addParam("sysparm_appId", g_form.getValue('RP_app_id'));
        ga.getXML(ajaxResponse);
    } else if (newValue != '') {
        g_form.showFieldMsg('RP_application_name', 'Field should not exceed more than 50 characters', 'error');
    }
}

function ajaxResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == true || answer == "true") {
        g_form.clearValue("RP_application_name");
        g_form.showFieldMsg('RP_application_name', 'Application Name already in use.', 'error');
    } else {
        g_form.hideFieldMsg('RP_application_name');
    }
}

 

how to fix this?

2 REPLIES 2

Shaik Dimple
Tera Expert

Hi @Amit Dey1 

 

This usually happens because the onChange script runs more than once. When you clear the field value inside the script, it triggers onChange again and the message gets hidden by hideFieldMsg, so it disappears after a second.

 

Try not to clear the field value in onChange. Instead just show the error when a duplicate is found and hide it only when the value is valid. Once you do that, the message should stay visible and behave consistently.

once try this script and let me know if you face any issues:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        g_form.hideFieldMsg('RP_application_name');
        return;
    }

    if (newValue.length > 50) {
        g_form.showFieldMsg('RP_application_name', 'Field should not exceed more than 50 characters', 'error');
        return;
    }

    var ga = new GlideAjax('utils');
    ga.addParam('sysparm_name', 'checkIfApplicationNameExists');
    ga.addParam('sysparm_appName', newValue);
    ga.addParam('sysparm_appId', g_form.getValue('RP_app_id'));
    ga.getXMLAnswer(function(answer) {
        if (answer === 'true') {
            g_form.showFieldMsg('RP_application_name', 'Application name already in use.', 'error');
        } else {
            g_form.hideFieldMsg('RP_application_name');
        }
    });
}

 

Thanks

Dimple

actually we have same code in one  catalog form also -

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) // || newValue == '')
    {
        return;
    }

    g_form.hideFieldMsg('cmdb_application_name');
    var length = g_form.getValue("cmdb_application_name").length;
    if (length <= 50 && newValue != '') {
        g_form.hideFieldMsg('cmdb_application_name');
        var ga = new GlideAjax("Utils");
        ga.addParam("sysparm_name", "checkIfApplicationNameExists");
        ga.addParam("sysparm_appName", newValue);
        ga.getXML(ajaxResponse);
    } else if (newValue != '') {
        g_form.showFieldMsg('cmdb_application_name', 'Field should not exceed more than 50 characters', 'error');
    }
}

function ajaxResponse(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == true || answer == "true") {
        g_form.clearValue("cmdb_application_name");
        g_form.showFieldMsg('cmdb_application_name', 'Application Name already in use.', 'error');
    } else {
        g_form.hideFieldMsg('cmdb_application_name');
    }
}
 
 
but this one is working fine, can you help please