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.

how to match the Worknotes values dynamically using Business rule

Ajay Singh2
Tera Contributor

Can anyone explain how to match the incident work notes values in Business rule to create a Parent/Child relationship.

 

MetricName: 'testing';

PodName: 'testing1';

 

Based on above these two values from work notes we need to create a parent/child relationship in Incident table.

 

Thank You!!

10 REPLIES 10

Hi @Tai Vu 

I tried below code and its working only issue is when I run this code for new MetricName and PodName which is not present in Journal table than I am getting below screenshot error.

 

(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] : null;
   var b = metricNameMatch ? metricNameMatch[1] : null;

   
   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.parent_incident = syselid; // updating child record
        current.update();
    }
    else {
            current.update();
    }
 })(current, previous);
 
In When to run condition I used after.
 
AjaySingh2_1-1727255170638.png

 

Thank You!!