The Zurich release has arrived! Interested in new features and functionalities? Click here for more

How to stop submission of a form based on the value returned by an asynchronous callback ?

PriyaVerma
Tera Contributor
I have written following code in Catalog Client Script. I want to stop submit record based on the value returned by ValidateAttachment. If ValidateAttachment returns false, I am using return false (to stop submit record). But this is not working as the issue is: Script continues to submit record before the callback returns response. 
 
 
 function onSubmit()  
{
      var id = '50eebf051bff35501738eb17ec4bcb59';
    // var id = g_form.getUniqueValue();
 
    // AJAX call to the server-side script include
    var ga = new GlideAjax('NGRPPortalUtilAjax');
    ga.addParam('sysparm_name', 'validateAttachment');
    ga.addParam('sysparm_app_id', id);
    ga.getXML(ValidateAttachment);
 
}

 

function ValidateAttachment(response)
{
    var answerFromXML= response.responseXML.documentElement.getAttribute("answer");
    var result = JSON.parse(answerFromXML);
    if(result=='true')
    {
        alert('Valid Insert' + result);
        return true;
    }
    else
    {
         alert("Please upload valid format" + result);
         return false;
    }
}
3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

I dont think getXML will work. Because it is asynchronous.

You need to use getXMLWait instead.


Please mark this response as correct or helpful if it assisted you with your question.

getXMLWait  does not work on Service Portal. 

There is an alternate solution provided in the below article, which may work for service portal.

 

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

 


Please mark this response as correct or helpful if it assisted you with your question.