Incident created from alert and getting value in work_notes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2024 11:56 PM
Hi All,
Incident is created from integration and based on that in work_notes getting MetricName and PodName values. So based on these two I have written Business Rule to make Parent-Child relationship and for this I used Journal table and everything is working as expected Parent-Child relation is created but issue is Child Incident can not identify the Closed and Active incident from journal table so it always make the last incident created as Parent which is long ago closed but we need the active incident to be Parent. Can any one any idea how to achieve this below code I used to make Parent-Child from Journal table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 12:03 AM
Hello,
I have made some modifications here -
Instead of querying the sys_journal_field for the last created incident, it now directly queries the incident table, making it more efficient and straightforward.
(function executeRule(current, previous /*null when async*/) {
var grJournal = new GlideRecord('sys_journal_field');
grJournal.addQuery('element_id', current.sys_id);
grJournal.addEncodedQuery('nameSTARTSWITHincident^element=work_notes^valueLIKEPodName^valueLIKEMetricName');
grJournal.orderByDesc('sys_created_on');
grJournal.query();
var payload = '';
if(grJournal.next()){
payload = grJournal.getValue('value');
}
var podNameRegex = /"PodName"\s*:\s*"([^"]+)"/;
var metricNameRegex = /"MetricName"\s*:\s*"([^"]+)"/;
var podNameMatch = payload.match(podNameRegex);
var metricNameMatch = payload.match(metricNameRegex);
var podName = podNameMatch ? podNameMatch[1] : '';
var metricName = metricNameMatch ? metricNameMatch[1] : '';
var grParentIncident = new GlideRecord('incident');
grParentIncident.addEncodedQuery('active=true^work_notesLIKE' + podName + '^work_notesLIKE' + metricName);
grParentIncident.orderByDesc('sys_created_on');
grParentIncident.setLimit(1);
grParentIncident.query();
if (grParentIncident.next() && current.sys_id != grParentIncident.sys_id) {
current.parent_incident = grParentIncident.sys_id;
current.update();
} else {
gs.log('No active parent incident found for incident: ' + current.number);
}
})(current, previous);
Please try this and let me know if it works
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 05:56 AM - edited 10-10-2024 06:12 AM
@Omkar Mone Its working but issue is like its just set the previous record as Parent but I need when Incident is not closed and metric and podname is matched it will be parent for all child, please see below screenshot. What need to be changed in code.
Thank You!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 06:30 PM
Hello Ajay,
T
(function executeRule(current, previous /*null when async*/) {
var grJournal = new GlideRecord('sys_journal_field');
grJournal.addQuery('element_id', current.sys_id);
grJournal.addEncodedQuery('nameSTARTSWITHincident^element=work_notes^valueLIKEPodName^valueLIKEMetricName');
grJournal.orderByDesc('sys_created_on');
grJournal.query();
var payload = '';
if(grJournal.next()){
payload = grJournal.getValue('value');
}
var podNameRegex = /"PodName"\s*:\s*"([^"]+)"/;
var metricNameRegex = /"MetricName"\s*:\s*"([^"]+)"/;
var podNameMatch = payload.match(podNameRegex);
var metricNameMatch = payload.match(metricNameRegex);
var podName = podNameMatch ? podNameMatch[1] : '';
var metricName = metricNameMatch ? metricNameMatch[1] : '';
var grParentIncident = new GlideRecord('incident');
grParentIncident.addEncodedQuery('active=true^work_notesLIKE' + podName + '^work_notesLIKE' + metricName);
grParentIncident.orderByDesc('sys_created_on');
grParentIncident.setLimit(1);
grParentIncident.query();
var parentIncidentId;
if (grParentIncident.next()) {
parentIncidentId = grParentIncident.sys_id;
}
if (parentIncidentId) {
var grChildIncidents = new GlideRecord('incident');
grChildIncidents.addEncodedQuery('active=true^parent_incidentISEMPTY^work_notesLIKE' + podName + '^work_notesLIKE' + metricName);
grChildIncidents.query();
while (grChildIncidents.next()) {
grChildIncidents.parent_incident = parentIncidentId;
grChildIncidents.update();
}
}
})(current, previous);
Let me know if it helped.
he below code will achieve that -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2024 02:49 AM
@Omkar Mone No its not working just inserting the new records.
