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

Hi @Sumit Singh 
Thanks for sharing the code. I did try to use the code that you have shared. It is still functioning the same. The issue that i am facing here is

1.When I add an application that is ready submitted/taken the assessment. It gives me a message stating "Assessment is already initiated for the application" - Which is correct

2. But when i give an application which is not taken the assessment i am able to submit the request the problem here is when i try to add another application for which the assessment is already taken along with the application for which the assessment is not taken. I am still able to submit the request.It should ideally not allow me to submit the request because one of the application assessment is already taken/submitted.

 

It is not even going inside the onSubmit script as i don't see any alert message for this scenario.

FYI: I am using the field List Collector to add/remove the applications on the catalog form.


Edited Client Script

 

function onSubmit() {

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

function ajaxCall() {

	alert("Inside ajaxCall");
	
    var ajx = new GlideAjax('CheckAssessment');
    ajx.addParam('sysparm_name', 'getApplications');
    ajx.addParam('sysparm_app', g_form.getValue('select_the_application_name'));
    ajx.getXMLAnswer(function(answer) {
		
		alert("answer: " + 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.ajaxComplete = true;
            g_form.submit();
        }
    });
}