Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Incident created from alert and getting value in work_notes

Ajay Singh2
Tera Contributor

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.

 

(function executeRule(current, previous /*null when async*/) {

// Script for Location field
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();
if(grJournal.next()){
    var payload = grJournal.getValue('value');
}

// Regular expressions to match PodName and MetricName
   var podNameRegex = /"PodName"\s*:\s*"([^"]+)"/;
   var metricNameRegex = /"MetricName"\s*:\s*"([^"]+)"/;
// Extracting values using regex
   var podNameMatch = payload.match(podNameRegex);
   var metricNameMatch = payload.match(metricNameRegex);

   var a = podNameMatch ? podNameMatch[1] : '';
   var b = metricNameMatch ? metricNameMatch[1] : '';


   
   var grJournalsec = new GlideRecord('sys_journal_field');
    grJournalsec.addEncodedQuery('valueLIKE' + a + '^valueLIKE' + b);
    grJournalsec.addEncodedQuery('nameSTARTSWITHincident^element=work_notes');
    grJournalsec.orderBy('sys_created_on');
    grJournalsec.setLimit(1);
    grJournalsec.query();

    var syselid;
    if(grJournalsec.next()) {
    syselid = grJournalsec.element_id;
}

    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id', current.sys_id);
    gr.query();
    if (gr.next() && current.sys_id != syselid) {
        //gs.log('current' + current.number + 'podname' + syselid);
        current.parent_incident = syselid; // updating child record
        current.update();
    }
    else {
            current.update();
    }
 })(current, previous);
 
 
Thank You!!
8 REPLIES 8

Omkar Mone
Mega Sage

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

@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.

AjaySingh2_1-1728565612622.png

 

 

Thank You!!

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 - 

@Omkar Mone No its not working just inserting the new records.