Need to update child incidents whenever work notes or state of parent incident changes

aamir1
Mega Expert

Hi All,

Requirement is to update the child incidents whenever there is any update in parent incident's work notes or state.

I am struggling with my code, it only updates the state but in case of work notes change, it is not working.

Kindly help finding the issue. Below is the code- 

 

BR- After

condition- current.isValidRecord()

code-

updateChildIncidents();
function updateChildIncidents() {
if (current.state.changesTo(6))
resolveChildIncidents();
else
updateChildren();
}

function updateChildren() {
var incState = current.state;
var fieldName= 'work_notes';

if(current.state.changes())
{
var chInc1 = new GlideRecord("incident");
chInc1.addQuery('parent',current.sys_id);
chInc1.addActiveQuery();
chInc1.addQuery('state','!=', '6');
chInc1.query();
while(chInc1.next()) {
chInc1.state = incState;
chInc1.update();
}
}

if(current.work_notes.changes()) {
msg = gs.getMessage('Work note copied from Parent Incident');
var rec2 = new GlideRecord("incident");
rec2.addQuery("parent", current.sys_id);
rec2.addQuery("state", "!=", "6");
rec2.addActiveQuery();
rec2.query();
while (rec2.next()) {
var fieldRawValue2 = current.getValue(fieldName) + '';
var fieldValue2 = fieldRawValue2.trim();
if (!fieldValue2 || fieldValue2.length <= 0)
return;
if (fieldRawValue2.indexOf(msg) == 0)
rec2[fieldName] = fieldRawValue2;
else
rec2[fieldName] = msg + ": " + fieldRawValue2;
rec2.update();
}
}
}

//
// Resolve active, unresolved incidents that are children of the current incident
//
function resolveChildIncidents() {
var incident = new GlideRecord("incident");
incident.addActiveQuery();
incident.addQuery("parent", current.sys_id);
incident.addQuery("incident_state", "!=", "6");
incident.query();
var msg = "";
while (incident.next()) {
gs.print("Incident " + incident.number + ' resolved based on resolution of Parent Incident ' + current.number);
incident.incident_state = 6;
if (incident.close_notes.nil()) {
msg = "Close notes copied from Parent Incident";
if (current.close_notes.toString().indexOf(msg) == 0)
incident.close_notes = current.close_notes;
else
incident.close_notes = msg + ": " + current.close_notes;
}
incident.close_code = current.close_code;
msg = "Resolved based on resolution of Parent Incident.";
if (current.comments.toString().indexOf(msg) == 0)
incident.comments = current.comments;
else
incident.comments = msg + " " + current.comments;
incident.work_notes = current.work_notes;
incident.update();
}
}

1 ACCEPTED SOLUTION

bill_blaney
Kilo Expert
Because work_notes is a journal field, it behaves differently. “After” the update, that field is blanked out. You will see different (more helpful) behavior with this field in a “Before” business rule (i.e. that field will still have a value you can use).

View solution in original post

6 REPLIES 6

Not applicable

Hi Aamir,

Please refer the OOTB business rule named "Update Child Incidents" .

 This business rule works after update and also updates the Work notes and Comment to the Child Incident.

Thanks,

Mark if correct/helpful

Regards,
Ajay

find_real_file.png

www.DXSherpa.com

aamir1
Mega Expert

Hi Ajay,

 

Thanks for your quick response!!

But I updated the same OOB BR.

 

Basically, my requirement is it should capture work notes changes as well as state changes. For state changes to Resolved, it should behave as it is, but when the state changes to any other state, it should also be reflected in the child.

 

Suppose state changes to Awaiting User Info, child state also should be changed to Awaiting user info.

bill_blaney
Kilo Expert
Because work_notes is a journal field, it behaves differently. “After” the update, that field is blanked out. You will see different (more helpful) behavior with this field in a “Before” business rule (i.e. that field will still have a value you can use).

Sandeep Kumar6
Giga Guru

Hi Amir,

 

I understood you requirement , Please change your condition and script as below it will work.

Note : Check your parameters as well.

 

Conditions : current.isValidRecord() && (current.incident_state.changesTo(IncidentState.RESOLVED) || current.comments.changes() || current.work_notes.changes())

 

updateChildIncidents();
function updateChildIncidents() {
if (current.incident_state.changesTo(IncidentState.RESOLVED))
resolveChildIncidents();
else {
updateChildren('comments', 'Comment copied from Parent Incident');
updateChildren('work_notes', 'Work note copied from Parent Incident');
}

}

function updateChildren(fieldName, msg) {
msg = gs.getMessage(msg);
var rec = new GlideRecord("incident");
rec.addQuery("parent_incident", current.sys_id);
rec.addQuery("incident_state", "!=", IncidentState.RESOLVED);
rec.addActiveQuery();
rec.query();
while (rec.next()) {
var fieldRawValue = current.getValue(fieldName) + '';
var fieldValue = fieldRawValue.trim();
if (!fieldValue || fieldValue.length <= 0)
return;
if (fieldRawValue.indexOf(msg) == 0)
rec[fieldName] = fieldRawValue;
else
rec[fieldName] = msg + ": " + fieldRawValue;
rec.update();
}
}

//
// Resolve active, unresolved incidents that are children of the current incident
//
function resolveChildIncidents() {
var incident = new GlideRecord("incident");
incident.addActiveQuery();
incident.addQuery("parent_incident", current.sys_id);
incident.addQuery("incident_state", "!=", IncidentState.RESOLVED);
incident.query();
var msg = "";
while (incident.next()) {
gs.print("Incident " + incident.number + ' resolved based on resolution of Parent Incident ' + current.number);
incident.incident_state = IncidentState.RESOLVED;
if (incident.close_notes.nil()) {
msg = "Close notes copied from Parent Incident";
if (current.close_notes.toString().indexOf(msg) == 0)
incident.close_notes = current.close_notes;
else
incident.close_notes = msg + ": " + current.close_notes;
}
incident.close_code = current.close_code;
msg = "Resolved based on resolution of Parent Incident.";
if (current.comments.toString().indexOf(msg) == 0)
incident.comments = current.comments;
else
incident.comments = msg + " " + current.comments;
incident.work_notes = current.work_notes;
incident.update();
}
}

 

Reference :  Update Child Incident Business Rule.

 

Please mark helpful or correct if it helps you.

Regards

Sandeep