Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

onSubmit client catalog script issue to stop submitting the form based on GlideAjax XML response

Drithika20
Tera Contributor

Hello, I have a requirement where I need to submit form based on the response from GlideAjax XML response in catalog client script. Here is my code :

function onSubmit() {
   alert(g_form.getValue('type_of_ser'));
   if(g_form.getValue('type_of_ser') === 'I want to travel out of country and require access to Microsoft apps.'){
    var ga = new GlideAjax('CheckDuplicateRequest');
    ga.addParam('sysparm_name','checkDuplicate');
    ga.addParam('sysparm_requested_for',g_user.userID);
    ga.addParam('sysparm_request_type',g_form.getValue('type_of_ser'));
    ga.addParam('sysparm_start_date',g_form.getValue('begin_date'));
    ga.addParam('sysparm_end_date',g_form.getValue('end_date'));
    ga.addParam('sysparm_country',g_form.getValue('travel_to_country'));
    ga.getXMLAnswer(function (response){
        alert(response);
        if(response == 'true'){
            g_form.addErrorMessage('Duplicate Request exists for same dates.');
            return false;
        }else{
        return true;
        }
       
    });
    //alert(answer);
   }
 
}
 
Please give me any valid solutions for this type of situation.
1 REPLY 1

Chaitanya ILCR
Mega Patron

Hi @Drithika20 

 

try this

function onSubmit() {
    if (g_scratchpad.isFormValid)
        return true;
    var actionName = g_form.getActionName();
    alert(g_form.getValue('type_of_ser'));
    if (g_form.getValue('type_of_ser') === 'I want to travel out of country and require access to Microsoft apps.') {
        var ga = new GlideAjax('CheckDuplicateRequest');
        ga.addParam('sysparm_name', 'checkDuplicate');
        ga.addParam('sysparm_requested_for', g_user.userID);
        ga.addParam('sysparm_request_type', g_form.getValue('type_of_ser'));
        ga.addParam('sysparm_start_date', g_form.getValue('begin_date'));
        ga.addParam('sysparm_end_date', g_form.getValue('end_date'));
        ga.addParam('sysparm_country', g_form.getValue('travel_to_country'));
        ga.getXMLAnswer(function(response) {
            alert(response);
            if (response == 'true') {
                g_form.addErrorMessage('Duplicate Request exists for same dates.');
                return false;
            } else {
                g_scratchpad.isFormValid = true;
                g_form.submit(actionName);
            }

        });
        return false;
        //alert(answer);
    }

}

refer this

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

 

 

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

Regards,
Chaitanya