Parent Incident work notes to be copied to child incidents

Indira8
Kilo Sage

Hi All,

We are facing an issue while updating the worknotes of a Parent Incident. 
Whenever the worknotes of the Parent incident is updated then the same has to be copied to the child incidents . However it is failing. We tried the out of box script as well as the custom script but both of them are failing. 

Could anyone please help what is the issue in the scripts. This defect has been pending from a very long time. 

The OOB BR is working in the PDI but not in our instance. Could you please help me with a solution. 

Business Rule : OOB - Update Child Incidents:

When - Before/after update ( both not working) 

Condition: current.isValidRecord() && (current.state.changesTo(IncidentState.RESOLVED) || current.comments.changes() || current.work_notes.changes()) && (current.child_incidents > 0) 

 

SCRIPT:

updateChildIncidents();

function updateChildIncidents() {
if (current.state.changesTo(IncidentState.RESOLVED))
resolveChildIncidents();
else {
var value;

if (current.comments.changes()) {
value = deriveFieldValue('comments', gs.getMessage('Comment copied from Parent Incident'));

if (value && value != "")
executeFlowAction(current, 'comments', value);
}

if (current.work_notes.changes()) {
value = deriveFieldValue('work_notes', gs.getMessage('Work note copied from Parent Incident'));

if (value && value != "")
executeFlowAction(current, 'work_notes', value);
}
}
gs.log("Function name - updateChildIncidents");
}

function executeFlowAction (parentIncidentGr, fieldName, value) {

try {
var inputs = {};
inputs['field'] = fieldName; // String
inputs['value'] = value; // String
inputs['parent_incident_gr'] = parentIncidentGr; // GlideRecord of table: incident
sn_fd.FlowAPI.getRunner().action('global.update_child_incidents').inBackground().withInputs(inputs).run();

} catch (ex) {
var message = ex.getMessage();
gs.error(message);
}
gs.log("Function name - executeFlowAction");
}

function deriveFieldValue(fieldName, msg) {

var fieldRawValue = current.getValue(fieldName) + '';
var fieldValue = fieldRawValue.trim();

if (fieldValue && fieldValue.length > 0) {

if (fieldRawValue.indexOf(msg) == 0)
return (fieldRawValue);
else
return (msg + ": " + fieldRawValue);
}
gs.log("Function name - deriveFieldValue");
return;
}

//
// Resolve active, unresolved incidents that are children of the current incident
//
function resolveChildIncidents() {
//check if update is valid or aborted before updating child incidents
if(current.isActionAborted())
return;

var incidentGR = new GlideRecord("incident");
incidentGR.addActiveQuery();
incidentGR.addQuery("parent_incident", current.sys_id);
incidentGR.addQuery("state", "!=", IncidentState.RESOLVED);
incidentGR.query();
var msg = "";
while (incidentGR.next()) {
gs.print("Incident " + incidentGR.number + ' resolved based on resolution of Parent Incident ' + current.number);
incidentGR.state = IncidentState.RESOLVED;
if (incidentGR.isValidField("close_notes") && incidentGR.close_notes.nil()) {
msg = gs.getMessage('{0} copied from Parent Incident', current.close_notes.getLabel());
if (current.close_notes.toString().indexOf(msg) == 0)
incidentGR.close_notes = current.close_notes;
else
incidentGR.close_notes = msg + ": " + current.close_notes;
}
if(incidentGR.isValidField("close_code"))
incidentGR.close_code = current.close_code;
msg = gs.getMessage("Resolved based on resolution of Parent Incident.");
if (current.comments.toString().indexOf(msg) == 0)
incidentGR.comments = current.comments;
else
incidentGR.comments = msg + " " + current.comments;
incidentGR.work_notes = current.work_notes;
incidentGR.update();
}
gs.log("Function name - resolveChildIncidents");
}

 

Business Rule 2: Customized:

 

Script :

updateChildIncidents();
function updateChildIncidents() {
if (current.state.changesTo(IncidentState.RESOLVED) || (current.state == IncidentState.RESOLVED && current.child_incidents.changes()))
resolveChildIncidents();
else {
updateChildren('comments', gs.getMessage('Comment copied from Parent Incident'));
updateChildren('work_notes', gs.getMessage('Work note copied from Parent Incident'));
}
}

function updateChildren(fieldName, msg) {
var rec = new GlideRecord("incident");
rec.addQuery("parent_incident", current.sys_id);
rec.addQuery("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() {
//check if update is valid or aborted before updating child incidents
if(current.isActionAborted())
return;

var incident = new GlideRecord("incident");
incident.addActiveQuery();
incident.addQuery("parent_incident", current.sys_id);
incident.addQuery("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.state = IncidentState.RESOLVED;
if (incident.close_notes.nil()) {
msg = gs.getMessage('{0} copied from Parent Incident', current.close_notes.getLabel());
if (current.close_notes.toString().indexOf(msg) == 0)
incident.close_notes = current.close_notes;
else
//incident.close_notes = msg + ": " + current.close_notes;
incident.close_notes = current.close_notes;
}
incident.close_code = current.close_code;
msg = gs.getMessage("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();
}
}

 

Thanks in advance

7 REPLIES 7

Mark Guldhammer
Giga Guru

Could you please update your question, and use the Insert Code Sample for all your code instead. It would make it a lot easier to read.

Hi @Mark Guldhammer  the question has been updated 

Awesome, much better. I suggest you try the steps Jorn is listing.

Jorn van Beek
Tera Guru

if it is working on a pdi but not your instance then it must be something in your instance. And if it is working on a pdi the code is good. Debugging on the code itself has no use.

Can you tell us which debug actions you have done? What was the result?

for example:

  • did you use the SN debugger / log statement at the start of the script to validate the BR does run?
  • did you test if the value that was copied was not an empty string?
  • did you validate there are no error messages in the logs when doing this?