Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

OnSubmit Catalog client script not working

Laxmi Sadhana K
Tera Contributor
function onSubmit() {
    var user = g_form.getValue('requestbytmp2_name');
    var ga = new GlideAjax('CheckOAuthToken');
    ga.addParam('sysparm_name', 'isTokenValid');
    ga.addParam('sysparm_user_id', user);
    ga.getXML(GetData);
   function GetData(response) {
        var ans = response.responseXML.documentElement.getAttribute("answer");
            if (ans == 'true') {
            alert('You have a valid token.');
            g_form.submit();
            return true;
        } else if (ans == 'false') {
            alert('You do not have a valid token. Please login to a new session before proceeding.');
            return false
        } else {
            alert('Unexpected response: ' + ans);
            return false
        }
    }

}
 
I am using this script to prevent form submission when isTokenValid returns false, but it is allowing the form submission. I tried with XML wait but it is not working. Is there a better way to achieve it?



2 REPLIES 2

Chaitanya ILCR
Mega Patron

Hi @Laxmi Sadhana K ,

 

Refer this article 

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

 

 

Please mark my response as helpful and accept it as solution if helped

Regards 

Chaitanya 

 

 

Hi @Laxmi Sadhana K ,

 

 

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var user = g_form.getValue('requestbytmp2_name');
    var ga = new GlideAjax('CheckOAuthToken');
    ga.addParam('sysparm_name', 'isTokenValid');
    ga.addParam('sysparm_user_id', user);
    ga.getXMLAnswer(function(ans) {
        if (ans == 'true' || ans == true) {
            alert('You have a valid token.');
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        } else if (ans == 'false') {
            alert('You do not have a valid token. Please login to a new session before proceeding.');
            return false;
        } else {
            alert('Unexpected response: ' + ans);
            return false;
        }
    });
    return false;
}

try this

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya