How to STOP form from submitting with GlideAjax?

andresgt
Mega Expert

Hello,

I have a case where I need to prevent the form from submitting depending on what is returned in the GlideAjax call.

I know asynchonous GlideAjax won't work because it submits without waiting for the response and Synchonous it not recomended and/or is not supported in ServicePortal.

Neither return false or g_form.submitting = false are working.

This is my current Client Script and Script Include with simple code just to find how the functionality can be.

Client Script

function onSubmit() {
	//Type appropriate comment here, and begin script below
	var result;
	var ga = new GlideAjax('ServiceCatalogAttachmentsAJAX');
	ga.addParam('sysparm_name', 'getAttachment');
	ga.getXML(callback);

        /*IF I USE RETURN FALSE HERE SCRIPT WORKS BUT I CAN'T WORK SOMETHING OUT SO CALLBACK FUNCTION RETURNS RESPONSE VALUE*/

	//return false;

	function callback(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
                return false;
	}
}

 

Script Include

var ServiceCatalogAttachmentsAJAX = Class.create();
ServiceCatalogAttachmentsAJAX.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	
	//Function to verify if x number of attachments are found on the form
	getAttachment: function(){
		return 'no';
	},
	
    type: 'ServiceCatalogAttachmentsAJAX'
});
1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Hi,

I have come across such scenarios where you need to use asynchronous calls in an onSubmit script but it does not work as expected. The alternate way is to forcefully trigger your onSubmit validations using on Change scripts which allows you to execute glide ajax calls asynchronously additionally providing support to service portal.

If you are trying to validate attachment count, then you could add a checkbox on your form asking users to check it to confirm the number of attachments. eg: I have attached all required attachments for my request approval.

You need to write this script when its value changes and then validate your attachment count asynchrnously. If the count does not match, uncheck the checkbox forcing users to attach the right numbers and then to revalidate it. I had followed this same approach for one of my customers.

Thanks!

View solution in original post

4 REPLIES 4

Alikutty A
Tera Sage

Hi,

Use this in your client script. This will move the scope of your callback function within the scope of onSubmit function

function onSubmit() {
	//Type appropriate comment here, and begin script below
	var result;
	var ga = new GlideAjax('ServiceCatalogAttachmentsAJAX');
	ga.addParam('sysparm_name', 'getAttachment');
	ga.getXML(function callback(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
                return false;
	});

        /*IF I USE RETURN FALSE HERE SCRIPT WORKS BUT I CAN'T WORK SOMETHING OUT SO CALLBACK FUNCTION RETURNS RESPONSE VALUE*/

	//return false;

	
}

Alikutty A
Tera Sage

Hi,

Try this out once

function onSubmit() {
	//Type appropriate comment here, and begin script below
	var result;
	var ga = new GlideAjax('ServiceCatalogAttachmentsAJAX');
	ga.addParam('sysparm_name', 'getAttachment');
	ga.getXML(function callback(response){
		var answer = response.responseXML.documentElement.getAttribute('answer');
                return false;
	});

        /*IF I USE RETURN FALSE HERE SCRIPT WORKS BUT I CAN'T WORK SOMETHING OUT SO CALLBACK FUNCTION RETURNS RESPONSE VALUE*/

	//return false;

	
}

Alikutty A
Tera Sage

Hi,

I have come across such scenarios where you need to use asynchronous calls in an onSubmit script but it does not work as expected. The alternate way is to forcefully trigger your onSubmit validations using on Change scripts which allows you to execute glide ajax calls asynchronously additionally providing support to service portal.

If you are trying to validate attachment count, then you could add a checkbox on your form asking users to check it to confirm the number of attachments. eg: I have attached all required attachments for my request approval.

You need to write this script when its value changes and then validate your attachment count asynchrnously. If the count does not match, uncheck the checkbox forcing users to attach the right numbers and then to revalidate it. I had followed this same approach for one of my customers.

Thanks!

Hi Alikutty,

I actually had the same idea, but thought there would be another way.

Maybe in future upgrades there will be a better way to do this.

 

Thanks a lot! 🙂