abort catalog submission if requested_for gender is female

Southsayer
Tera Expert

Hello everyone,

this is the code I have written but I am unable to abort catalog form submission. What might be the issue?

//Client Script

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var ga = new GlideAjax('abortfemalerequester');
   ga.addParam('sysparm_name', 'getreqGender');
   ga.addParam('sysparm_id', g_form.getValue('requested_for'));
   ga.getXML(getResponse);

   function getResponse(response){
	var answer = response.responseXML.documentElement.getAttribute('answer');
	alert (answer);
	if (answer == "true"){
		return true;
	}else {
		return false;
	}
   }
}

//SCRIPT INCLUDE
var abortfemalerequester = Class.create();
abortfemalerequester.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getreqGender: function() {
        var gr = new GlideRecord('sys_user');
		var reqId = this.getParameter('sysparm_id');
		gr.addQuery('sys_id', reqId);
		gr.query();
		if (gr.next()){
			gr.log('Gender is: ' + gr.gender);
			var gender = gr.gender;
			var ans;
			if (gender == 'Male'){
				gs.log('You are inside male condition');
				return (true);
			}else if (gender == "Female"){
				gs.log('You are inside female condition');
				return (false);
			}
		}

    },
    type: 'abortfemalerequester'
});
6 REPLIES 6

Amit Verma
Kilo Patron
Kilo Patron

Hi @Southsayer 

 

Below post could be helpful :

https://www.servicenow.com/community/developer-forum/calling-a-script-include-from-onsubmit-client-s...

 

Probably, your Catalog item is getting submitted before you get a response from the script include. Please refer the above post and try modifying your Script Include and Client Script accordingly. Also, as suggested by @Anurag Tripathi , you could have used an On-Change Client Script as well where On-Change of Requested for Variable, you can call your Script Include to get the gender and set a hidden field accordingly in your catalog item. In the On-Submit Client Script, you can refer to the value of the hidden field to decide whether to allow the submission or not.

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Rohini S Sane
Tera Guru

Hi @Southsayer ,

Asynchronous GlideAjax not works for onSubmit client script. Instead of using asynchronous use synchronous glide ajax with getXMLWait() .

 

 

Regards,

Rohini Sane