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

Anurag Tripathi
Mega Patron
Mega Patron

Hi, 

GlideAjax with Callback doesn't work well on onSubmit script, why don't you run this same thing on change of  requested_for field.

 

Also see this -

How to do async validation in an onsubmit client script. - Support and Troubleshooting (servicenow.c...

-Anurag

Akshay Gupta2
Kilo Sage

Hi @Southsayer 

 

The issue in your code is that you are returning true or false from the getreqGender function in the script include, but the onSubmit client script expects a GlideRecord object to be returned in order to abort the form submission.

To abort the form submission, you need to throw a GlideRecordAbortException exception in the getreqGender function when the requested user's gender is female. Here's the updated code:

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');
                throw new GlideRecordAbortException();
            }
        }

    },
    type: 'abortfemalerequester'
});

When the getreqGender function throws a GlideRecordAbortException exception, the form submission will be aborted.

 

Do mark this as helpfull, if this answers your query.

 

Regards

Akshay Gupta

Hello Akshay, I tried this but it didn't abort for females. When I tried to open this definition GlideRecordAbortException, it said no definition found. could you help?

Hi @Southsayer ,

 

You can try this solution and check if it works for you.

 

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

 

Regards,

Akshay Gupta