OnSubmit Client script validation on catalog item

Ankita9793
Tera Contributor

Hi All,

 

I have written a client script to validate the response from an API, if the response is False then it should return false and not allow user to submit the form. But it is not working as expected, Please suggest.

 

function onSubmit() {

    var empNo = g_form.getBeneficiaryData('employee_id');

    //hsbcShowHideLoaderCatalog();

 

    var vdiAjax = new GlideAjax('tuVDIRequestHelper');

    vdiAjax.addParam('sysparm_name', 'getOSTFileSize');

    vdiAjax.addParam('sysparm_emp_id', empNo);

   var x = vdiAjax.getXMLAnswer(function(answer) {

 

        if (answer == 'false') {

           

            alert('VDI can not be requested as file size too large for VDI.');

            g_form.setVisible('faq', true);

            return false; // Return False does not work

        }

    });

 

    alert('x' +x);

 

   return ;

}

 

2 REPLIES 2

Shruti
Mega Sage
Mega Sage

Hi,

GlideAjax (Asynchronous  call) does not work on onSubmit Client Script.

Try to use getXMLWait() for synchronous call so the execution waits for response and the form will not be submitted

swathisarang98
Giga Sage
Giga Sage

Hi @Ankita9793 ,

 

If you call a script include on submit, it must be a Synchronous call. Using getXMLWait. Otherwise the Catalog item will be submitted before you get a response from the script include

 

Support article,

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579#:~:text=The%20GlideAj... 

 

servicenow article:

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta... 

function onSubmit() {

    var empNo = g_form.getBeneficiaryData('employee_id');

    //hsbcShowHideLoaderCatalog();

 

    var vdiAjax = new GlideAjax('tuVDIRequestHelper');

    vdiAjax.addParam('sysparm_name', 'getOSTFileSize');

    vdiAjax.addParam('sysparm_emp_id', empNo);

   var x = vdiAjax.getXMLAnswer(answer); 

 

        if (answer == 'false') {

           

            alert('VDI can not be requested as file size too large for VDI.');

            g_form.setVisible('faq', true);

            return false; // Return False does not work

        }

    

   

    alert('x' +x);

 

   return ;

}