how to prevent submit with error message?

Akki1
Tera Contributor

i'm using a client script with the below code. I want to popup a error message and prevent submit if my output is false;

var output='';

var req=g_user,userName;

var ent=g_form,getValue('gp_name);

var acc=new GlideAjax('global.app_validate');

acc.addParam(sysparm_name,'validate');

acc.addParam('gp',ent);

acc.addParam('user',req);

acc.getXMLAnswer(function(response){

output=response;

}

 

 

6 REPLIES 6

Sagar Pagar
Tera Patron

Hi @Akki1,

Try the alternative (Display Business rule and on submit client scripts) shared by Akshay.

 

Thanks,
Sagar Pagar

The world works with ServiceNow

muneermajeed
Tera Contributor

Hi Akki1 your code is not worked because of the asynchronous behavior of GlidAjax API the glidajax function is execut in last that's why the form is always submitted even the response is true or false so to prevent the form submission you need to used a flag variable like that here's the code

'

var submitted = false;

function onSubmit() {
    if (submitted) {

        return true;
    }
    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('check_department_active_incident_advance_task_02');
    ga.addParam('sysparm_name', 'preventrecord');
    ga.addParam('sysparm_id', g_form.getValue('caller_id'));

    ga.getXMLAnswer(function(res) {

        alert(res);
        if (res == 'true') {
            g_form.addErrorMessage("This Department has already created 5 active incident from the previous 7 days .. ");
            // submitted = true;
        } else {
            submitted = true;
            g_form.submit();
            g_form.addInfoMessage("form submitting...");
        }
    });

    return false;

}'