The CreatorCon Call for Content is officially open! Get started here.

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

Hello Anvesh,

    First of all thanks to your unwavering dedication to get my doubt resolved. Since it worked on your PDI it should have worked on mine as well but it is not the case(I copy pasted your code-to be on sure side). That g_scratchpad I had tried yesterday.  Today when I kept UI Type as 'All' or 'Mobile' it is letting me submit the record.  But when I kept 'Desktop' it gave me an error g_scratchpad is not defined. 

FYI, I am using Utah version and providing you with the following configuration details for the same. 

 

maheshkhatal_0-1695732082735.png

Error: when Desktop is option=>Error MessageonSubmit script error: ReferenceError: g_scratchpad is not defined:
function () { [native code] }

 

Else it goes on submitting the code.

 

Thank you once again.

Mahesh.

 

 

Hi @maheshkhatal 

Are you using this Record Producer in Platform UI or Service Portal? Or Both ?

Thanks,
Anvesh

Hi @AnveshKumar M ,

   This time tested from Service Portal. It worked. May be I should have tried earlier. It's bit weird. Thanks though.

Mahesh.

It worked when I submitted from Service Portal. Thank you again.