Don't create incident if already. Incident Exists with same CI..?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2023 11:38 PM
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());
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2023 09:05 PM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2023 09:14 PM - edited 01-29-2023 09:42 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2023 09:24 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2023 09:40 PM
Can you explain your business requirement in detail?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader