Alert on catalog item

I_Das
Tera Contributor

Hello All,

I have a requirement where if a ci had an outage it will show alert when an incident will be raised for the same ci on employee portal. So I wrote a script include and catalog client script. The catalog client script works. It is showing me alert and the form is not submitting. But after that when I am trying to raise another incident based on other ci which does not have an outage incident form is not submitting.

 

Catalog client script:

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var configItem = g_form.getValue('cmdb_ci'); // Make a GlideAjax call to check if the CI has an outage
var ga = new GlideAjax('CheckOutageScriptInclude');
ga.addParam('sysparm_name', 'getXML');
ga.addParam('sysparm_cmdb_ci', configItem);
ga.getXMLAnswer(function(response)
{if (response == 'true') {
alert('This CI has an outage. Incident cannot be raised.');
g_form.clearMessages(); // Clear any previous messages
return false; // Prevent form submission
} else
{
return true; // Allow form submission
}
}); // Ensure the form submission is halted until the response is received
return false;
}
 
Script include:
var CheckOutageScriptInclude = Class.create();
CheckOutageScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getXML: function() {
var configItem = this.getParameter('sysparm_cmdb_ci');
var hasOutage = this._checkForOutage(configItem);
return hasOutage.toString();
}, _checkForOutage: function(configItem) {
var outageGr = new GlideRecord('cmdb_ci_outage');
outageGr.addQuery('cmdb_ci', configItem);
outageGr.addQuery('begin', '<', new GlideDateTime());
outageGr.addQuery('end', '>', new GlideDateTime());
outageGr.query(); return outageGr.hasNext();
} });
   
How to fix this issue?
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

Due to its asynchronous nature, you cannot use getXML... in an onSubmit Catalog Client Script to prevent submission of the form.  The form submission continues without waiting for the server response.  In your case the final return false is always preventing the form submission.  There are numerous posts on work-arounds.  The most straight-forward is to move the Glide Ajax call to an onChange client script when a mandatory variable changes, then clear a mandatory variable if the alert is triggered, thereby preventing submission of the form.