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

Hello Ajay,

 

Can you specify what is not working? As I have only added the last part differently where it querys all the incidents matching the work notes text. Can you put some logs around and let me know? As I can see the background script working for me - 

 

OmkarMukeshMo_0-1728640782031.png

 

 

@Omkar Mone 

In when to run condition i used below condition see screenshot.

 

AjaySingh2_0-1728645822418.png

 

While creating Incident getting same incident as parent.

 

AjaySingh2_2-1728652820317.png

 

 

These errors are also visible.

AjaySingh2_3-1728652859973.png

 

 

Creating Incident from Rest Api Explorer

AjaySingh2_0-1728652474860.png

 

While getting alert to create Incident form MetricName and PodName values are coming like this see below screenshot.

 

AjaySingh2_1-1728652679695.png

 

Thank You!!

Hello Ajay,

 

I see, I tried this version in my PDI and its returning the values as expected. Can you give it a try?

(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 grJournalsec = new GlideRecord('sys_journal_field');
    grJournalsec.addEncodedQuery('valueLIKE' + podName + '^valueLIKE' + metricName);
    grJournalsec.addEncodedQuery('nameSTARTSWITHincident^element=work_notes^element_id!=' + current.sys_id);
    grJournalsec.orderByDesc('sys_created_on');
    grJournalsec.setLimit(1);
    grJournalsec.query();

    var parentIncidentId;
    if (grJournalsec.next()) {
        parentIncidentId = grJournalsec.getValue('element_id');
    }
gs.info("Printing the parent sysid - " +parentIncidentId)
    if (parentIncidentId) {
        current.parent_incident = parentIncidentId;
        
        current.update(); //see if you want to remove this as BR is on before insert/update as I think its not needed.
    }

})(current, previous);

@Omkar Mone Not its still not working. Now only Incident is created one bye one no relation.

 

Thank You!!