While submitting the request we need display error message if record is exist

Balasai peela
Tera Contributor

Hi Team,

 

We have a record producer in that subject common name field name is available, while submitting the request if certificate is exists we need to cancel the form and needs to display error message like this certificate already exist.

I have used below code but it is not working.

 

Script include:

var Validate_SCN = Class.create();
Validate_SCN.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    validatescn: function() {
var subject_common_name = this.getParameter('subject_common_name');
var userGR = new GlideRecord('cmdb_ci_certificate');
userGR.addQuery('subject_common_name', subject_common_name);
userGR.query();
if (userGR.next()) {
    gs.log('certificate is: ' + userGR.subject_common_name);
            var certificate = userGR.subject_common_name;
            //var ans;
            if (certificate == 'true'){
                gs.log('Certificate alredy exist');
                return false;
            }if (certificate == "false"){
                gs.log('Certificate not alredy wexist');
                return true;
            }

}


},


    type: 'Validate_SCN'
});
 
Client script:
function onSubmit() {
   //Type appropriate comment here, and begin script below
    var usrGA = new GlideAjax('Validate_SCN');
    usrGA.addParam('sysparm_name', 'validatescn');
    //usrGA.addParam('sysparm_subject_common_name', newValue);
    usrGA.addParam('sysparm_subject_common_name', g_form.getValue ('subject_common_name'));
     usrGA.getXML(getResponse);

function getResponse(response){
    var answer = response.responseXML.documentElement.getAttribute('subject_common_name');
    alert (answer);
    if (answer == "true"){
        alert('Certificate not exist');
        return true;
    }else {
        alert('Certificate exist');
        return false;
    }

   
       
    }
   

   
}
 
Could you please help me on this.
 
Thanks in Adv.
 
Regards,
Sai
2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Balasai peela GlideAjax is an asynchronous call and when you use it on the onSubmit script, it doesn't block the UI, hence by the time you get the API response the form already gets submitted.

 

In this case you need to update your onSubmit script based on this article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0779964

 

You can also draw inspiration from this article https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-...

 

Please update your script accordingly and the form will be validated correctly. 

Hi Sandeep,

 

Thanks for your quick response.

 

Could you please provide code on this.

 

Regards,

Sai