Prevent form submission using onSubmit catalog client script

Hari1
Mega Sage

Hi,

I have a requirement to validate the options selected from the list collector against an existing table if the table record entry already has the selected option from list collector then it should not allow me to submit the request. For Example: (I have already submitted a request for the application assessment. I am Gliding through the existing table record to see if the application is already present and if it is present i should not be able to submit the catalog request else it should allow me to submit the request). I am able to validate only once. i.e If I add new value or remove old value of an option for the list collector it does not work. Validation happens only 1st time.
Below is script:

Catalog Client Script: OnSubmit

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
	
	var appList = g_form.getValue('select_the_application_name');
	
	alert("appList: " + appList);
	
    var ajx = new GlideAjax('CheckAssessment');
    ajx.addParam('sysparm_name', 'getApplications');
    ajx.addParam('sysparm_app', g_form.getValue('select_the_application_name'));
    ajx.getXMLAnswer(getNewUserDetails);
    return false;
	
    function getNewUserDetails(answer) {

        if (answer != "No Application Found") {
            //var answer = response.responseXML.documentElement.getAttribute("answer");
            var answerArr = answer.toString().split(',');

            alert("answerArr: " + answerArr + " ,answerArr.length: " + answerArr.length);
            g_form.addErrorMessage('Assessment is already initiated for the application ' + answerArr);
            return false;
        } else {
            g_form.submitted = true;
            g_scratchpad.isFormValid = true;
            g_form.submit(g_form.getActionName());
            return true;
        }
	}
}

Script Include:

var CheckAssessment = Class.create();
CheckAssessment.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    getApplications: function() {

        gs.info("Assessment - Inside Script Include");

        var assmtTaken = [];
        var getApplicationSysId = this.getParameter('sysparm_app');

        gs.info("Assessment - getApplicationSysId: " + getApplicationSysId);

        getApplicationSysId = getApplicationSysId.toString().split(',');

        gs.info("Assessment - getApplicationSysId.length: " + getApplicationSysId.length);

        for (var i = 0; i < getApplicationSysId.length; i++) {

			var applicationName;
			
            gs.info("Assessment - getApplicationSysId: " + getApplicationSysId[i] + " ,i value is: " + i);

            var appName = new GlideRecord("cmdb_ci_business_app");
            appName.addQuery("sys_id", getApplicationSysId[i]);
            appName.query();
            if (appName.next()) {
                applicationName = appName.getValue("name");
                gs.info("Assessment - applicationName: " + applicationName);
            }
			var queryVal = "metric=3rtgefrt3b8f551434545fe2b24bcb48^category=b55c2dgfdrtf551gfd456da02b24bcb90^string_value=" + applicationName;

            var app = new GlideRecord("asmt_assessment_instance_question");
            app.addEncodedQuery(queryVal);
            app.query();
            if (app.next()) {
                gs.info("Assessment - applicationName Found");
                assmtTaken.push(applicationName); 
            }
        }
		
		if(assmtTaken.length > 0){
			return assmtTaken.toString();
		}else{
			return "No Application Found";
		}
    },
    type: 'CheckAssessment'
});

 

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Duplicate post: https://www.servicenow.com/community/it-service-management-forum/how-to-check-if-the-selected-option... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi @Allen Andreas 

Thanks for highlighting the duplicate post, but do you have any solution to propose for the above scenario?

Hi @Allen Andreas 

Thanks for highlighting the duplicate post, but do you have any solution to propose for the above scenario?

Sumit Singh
Tera Expert

Hi @Hari1 
I was facing the same issue. I have fixed using g_form.ajaxComplete
I am sharing the script please take reference.

function onSubmit() {
    if (!g_form.ajaxComplete) {
        ajaxCall();
        return false;
    }
}



function ajaxCall() {
    
    //initialize glideajax here
    ga.getXMLAnswer(function(answer){
        if(answer==true || answer=="true"){
            g_form.ajaxComplete = true;
            g_form.submit();
        }else if(answer== false || answer=="false"){
            g_form.addErrorMessage("error message");
        }

    });
}