Abort Record Producer on Incident table if an Incident with select Configuration Item already exits

maheshkhatal
Mega Sage
Mega Sage

Hello All,

  I want to Abort(not let record submit) record submission through Record producer on Incident table if the selected 

Configuration Item already exists with a particular incident. I have gone through same issues here but none of them were helpful. I have tried both catalog client onChange and onSubmit sctipt but none to avail. 

 

Catalog onChange Client Script:

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
  var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci',newValue);
//alert(g_form.getValue('cmdb_ci'));
//Tried only ga.getXML(submitRecord)

var myanswer = ga.getXML(submitRecord);
return myanswer;

function submitRecord(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    return answer;
}
   
}

 

 

 

Script include:

 

 

var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	isCIAvailable: function() {
        var incGr = new GlideRecord('incident');
        incGr.addQuery('cmdb_ci',this.getParameter('sysparm_ci'));
		incGr.query();
		gs.log('inside the ci check si');
        if(incGr.next()){
			return true;
		}else{
			return false;
		}

    },

    type: 'checkCIExistIncident'
});

 

 

Any input in this regard will be greatly appreciated.

 

Thank you,

Mahesh.

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @maheshkhatal ,

I have tried simulating your issue in my PDI today and finally found the working code for your issue. Please try with the following Script Include and Client Script.

 

Script Include:

 

var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isCIAvailable: function() {
		gs.info("MAK: Hello");
        var incGr = new GlideRecord('incident');
        incGr.addQuery('cmdb_ci', this.getParameter('sysparm_ci'));
        incGr.addQuery('active', true);
        incGr.query();
        if (incGr.next()) {
            return 'true';
        } else {
            return 'false';
        }
    },

    type: 'checkCIExistIncident'
});

 

Client Script:

 

function onSubmit() {
    if (g_scratchpad.isFormValidAjax) {
        g_scratchpad.isFormValidAjax = null;
        return true;
    }
    
    g_scratchpad.isFormValidAjax = false;
    var cmdb_ci = g_form.getValue('cmdb_ci'); //change variable as per your configuration
    var ga = new GlideAjax('global.checkCIExistIncident');
    ga.addParam('sysparm_name', 'isCIAvailable');
    ga.addParam('sysparm_ci', cmdb_ci);
    ga.getXMLAnswer(setAnswer);
    return false;


    function setAnswer(answer) {
        if (answer) {
            if (answer == 'true') {
				g_form.addErrorMessage("There is an Incidnet for this CI");
                return false;
            } else {
                var actionName = g_form.getActionName();
                g_scratchpad.isFormValidAjax = true;
                g_form.submit(actionName);
            }
        }
    }
}

 

Please mark my answer helpful and accept as solution if it worked for you 👍✔️

 

 

Thanks,
Anvesh

View solution in original post

13 REPLIES 13

Anand Kumar P
Tera Patron

Hi @maheshkhatal ,
Update Onsubmit client script like below.

function onSubmit() {
var cmdb_ci = g_form.getValue('cmdb_ci');

var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci', cmdb_ci); 

ga.getXML(submitRecordProc);

return false;// Always return false here to prevent the form from submitting before the callback finishes
}

function submitRecordProc(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer == 'false') {
// If there's no matching incident, you can allow the form to submit
g_form.submit();
} else {

alert('An incident with the same CI already exists.');
return false;
}
}
Script Include--

var checkCIExistIncident = Class.create();

checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

 

 isCIAvailable: function() {

        var incGr = new GlideRecord('incident');

        incGr.addQuery('cmdb_ci',this.getParameter('sysparm_ci'));

     incGr.addQuery('active', true);

  incGr.query();

  gs.log('inside the ci check si');

        if(incGr.next()){

   return 'true';

  }else{

   return 'false';

  }

 

    },

 

    type: 'checkCIExistIncident'

});

Thanks,

Anand



It checks properly for existing CI and if exists I have added error message. But when CI item is new and goes for submitting it is giving error saying '

The g_form.submit function has no meaning on a catlog item. Perhaps you mean g_form.addToCart() or g_form.orderNow() instead'. How can I get rid off this error.
 
Thank you.

Hi @maheshkhatal,

 

g_form.submit()/g_form.save(). When you use this in Native UI then you migh get error "The g_form.submit function has no meaning on a catlog item.
Try to use g_form.addToCart() or g_form.orderNow() instead"
.

 

Refer bbelow linkhttps://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta... 

 

 

 

 

Please mark it helpful and accepted solution if it’s helpful..

 

Thanks,

Anand

 

Thanks,

Anand

Did try g_form.addToCart() or g_form.orderNow() instead of g_form.submit()/save but when I click on the submit button no UI action happens. 

AnveshKumar M
Tera Sage
Tera Sage

Hi @maheshkhatal ,

I have tried simulating your issue in my PDI today and finally found the working code for your issue. Please try with the following Script Include and Client Script.

 

Script Include:

 

var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isCIAvailable: function() {
		gs.info("MAK: Hello");
        var incGr = new GlideRecord('incident');
        incGr.addQuery('cmdb_ci', this.getParameter('sysparm_ci'));
        incGr.addQuery('active', true);
        incGr.query();
        if (incGr.next()) {
            return 'true';
        } else {
            return 'false';
        }
    },

    type: 'checkCIExistIncident'
});

 

Client Script:

 

function onSubmit() {
    if (g_scratchpad.isFormValidAjax) {
        g_scratchpad.isFormValidAjax = null;
        return true;
    }
    
    g_scratchpad.isFormValidAjax = false;
    var cmdb_ci = g_form.getValue('cmdb_ci'); //change variable as per your configuration
    var ga = new GlideAjax('global.checkCIExistIncident');
    ga.addParam('sysparm_name', 'isCIAvailable');
    ga.addParam('sysparm_ci', cmdb_ci);
    ga.getXMLAnswer(setAnswer);
    return false;


    function setAnswer(answer) {
        if (answer) {
            if (answer == 'true') {
				g_form.addErrorMessage("There is an Incidnet for this CI");
                return false;
            } else {
                var actionName = g_form.getActionName();
                g_scratchpad.isFormValidAjax = true;
                g_form.submit(actionName);
            }
        }
    }
}

 

Please mark my answer helpful and accept as solution if it worked for you 👍✔️

 

 

Thanks,
Anvesh