How to use GlideAjax in on submit client script

KARAN24
Tera Contributor

Hi Team,

I have to do a validation on one variable called tax id ,if the tax id  does not exists in account table ,it  clear out the value, and is happening with On change client script.But it should not allow to submit the form.But form submission is possible.How can we validate that field on oN Submit.

 

Thanks,

Karan

13 REPLIES 13

Aman Kumar S
Kilo Patron

Hi @KARAN24 ,

This has to be achieved by sync ajax using getXMLWait function or if you still want to use the async one follow below options.
Refer following articles:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579

https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-...

 

Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

@KARAN24 

why not clear that variable and make it mandatory when the validation fails.

With this user can submit the form only when correct value is present in that variable tax id

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

I have an On change client script which is clearing the variable value,but the issue is if the user enters the value and directly hits on submit button the form gets submitted.so need a validation for this scenario.

Here is the script include and client script:

Script Include:

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

    getCampus: function () {
        var taxid = this.getParameter('sysparm_u_party_tax_id');
        
        
        var tax1 = new GlideRecord('customer_account');
        tax1.addEncodedQuery('u_party_tax_id!=NULL');
        tax1.query();
        var arr_final=[];
    while(tax1.next())
    {
        var arr=tax1.u_party_tax_id.split(',');
        
        for(var i=0;i<arr.length;i++)
        {
            if(arr[i]==taxid)
            {
                arr_final.push(1);
            }
        }
        
    }
   return arr_final.length;
        
      
            
        
    
    },
    

                                        
    
    

    isPublic:function(){return true;},
    type: 'getTax'
});
 
On Change Client script:
function onChange(control, oldValue, newValue, isLoading) {
   if ( newValue == '') {
      return;
   }
var user_id = g_form.getValue('u_party_tax_id');
    var a=user_id.toUpperCase();
    //alert(a);
    //g_form.addInfoMessage("Timer:" +user_id);
    var ga = new GlideAjax('sn_customerservice.getTax');
    ga.addParam('sysparm_name''getCampus');
    ga.addParam('sysparm_u_party_tax_id',a);
    ga.getXML(NameDetails);
    function NameDetails(response){
        //alert("testalert");
        var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
        
         if (answer >= 1) {
           // g_form.addInfoMessage("Verified");
              
             g_form.setValue('u_party_tax_id',a);
            
            
        } 
        else 
        {
            //g_form.addErrorMessage("Not Verified");
             alert('Please enter Valid Tax ID');
            g_form.clearValue('u_party_tax_id');
            g_form.showFieldMsg('u_party_tax_id''Please enter Valid Tax ID','error',true);
            
            
            
            
        }
        
     
    
    }
}

//      if (answer==a){
//          g_form.setValue('u_party_tax_id',answer);
//      }


//      else{
//      g_form.clearValue('u_party_tax_id');
//          //g_form.showFieldMsg('Please enter Valid Tax ID');
//      g_form.showFieldMsg('u_party_tax_id', 'Please enter Valid Tax ID','error',true);
//  }
    


      
    //}

    //}

    
   //Type appropriate comment here, and begin script below
   

@KARAN24 

if you are showing an error box near that variable then it won't allow submission

OR

you can clear it and make it mandatory

function onChange(control, oldValue, newValue, isLoading) {
if ( newValue == '') {
return;
}
var user_id = g_form.getValue('u_party_tax_id');
var a=user_id.toUpperCase();
//alert(a);
//g_form.addInfoMessage("Timer:" +user_id);
var ga = new GlideAjax('sn_customerservice.getTax');
ga.addParam('sysparm_name', 'getCampus');
ga.addParam('sysparm_u_party_tax_id',a);
ga.getXML(NameDetails);
function NameDetails(response){
//alert("testalert");
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);

if (answer >= 1) {
// g_form.addInfoMessage("Verified");
g_form.setValue('u_party_tax_id',a);
}
else
{
alert('Please enter Valid Tax ID');
g_form.clearValue('u_party_tax_id');
g_form.setMandatory('u_party_tax_id', true);
}
}
}

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader