Don't create incident if already. Incident Exists with same CI..?

Mannam Praveen
Tera Expert

Hi All

 

Don't create incident if already. Incident Exists with same CI..? on the incident table. If any incident created by event or any other way. That's should be attached to Already existing incident. How we can achieve this please help

 

var incGR = new GlideRecord('incident');
incGR.addQuery("cmdb_ci",current.cmdb_ci);
incGR.orderBy('sys_created_on');
if(incGR.next()){
createChild(incGR);
}

function createChild(obj){
var childGR = new GlideRecord('incident');
childGR.initialize();
childGR.parent = obj.getUniqueValue();
// add more field to copy from parent
childGR.insert();
gs.addInfoMessage('Another incident already exists. New ticket has been added under '+obj.number);
current.setAbortAction(true);
gs.setRedirect(obj.getLink());
}

 

4 REPLIES 4

Rahul RJ
Giga Sage
Giga Sage

@Mannam Praveen   if you don't want create incident exist with same CI then you need to write before insert business rule

var incGR = new GlideRecord('incident');
incGR.addQuery("cmdb_ci",current.cmdb_ci);
incGR.orderBy('sys_created_on');
if(incGR.next()){
gs.addInfoMessage("Incident already exists for "+incGr.getDisplayValue('cmdb_ci')+" incident number "incGr.number);
current.setAbortAction(true);
}

Regards,

RJ

Tony Chatfield1
Kilo Patron

Hi, your post states that you do not want to create an Incident if one exists, then your code shows creation of a child incident, am I correct in assuming that you want to create an Incident but set it as a child?
Also you do not indicate where\how this script runs I am guessing it is a before insert BR?
If yes, then I do not think that you want\need the childGR function or the setAbortAction() 
as you just need to add something like this (untested) to your existing Incident create BR in order to set the parent field.

 

var incGR = new GlideRecord('incident');
//Only check for active Incidents
incGR.addActiveQuery();
incGR.addQuery("cmdb_ci",current.cmdb_ci);
//exclude resolved Incidents?
incGR.addQuery('state', '!=', 7);
//this orderBy is not necessary if you only have 1 result.
incGR.orderBy('sys_created_on');

incGR.query()
if(incGR.next()){
current.parent = incGR.sys_id;
gs.addInfoMessage('Another incident already exists. New ticket has been added under ' + incGR.number);
}

 

 

Edit: you might want to use 'incident_state' rather than 'state' and I think the OOB value for Resolved is 6, not 7

ajit kasabale
Tera Expert

Hello Mannam,

You can write before business rule on insert action.
Please find below code 

var gr = new GlideRecord('incident');
gr.addActiveQuery();
gr.addQuery("cmdb_ci",current.cmdb_ci);
gr.addQuery('state', '!=', 7);
gr.orderBy('sys_created_on');

gr.query()
if(gr.next()){

current.setAbortAction(true);
gs.addInfoMessage('Another incident already exists ' + gr.number);
}

Ankur Bawiskar
Tera Patron
Tera Patron

@Mannam Praveen  

Can you explain your business requirement in detail?

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader