abort catalog submission if requested_for gender is female
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 05:30 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 05:33 AM - edited ‎02-20-2024 05:34 AM
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 -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2024 08:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 01:02 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-28-2024 01:15 AM
