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

Hi @Jorn van Beek . Could you help me with the logs statements where to be placed in the functions 

Thank you 

if you go with the OOB version there is already a log action see line:

gs.log("Function name - updateChildIncidents");

so question is:

after updating the comments of the parent do you see a log entry with the text

Function name - updateChildIncidents

That should give you an indication if the business rule is running or not.

 

if the business rule is running, then place a log statement like this one

var inputs = {};
inputs['field'] = fieldName; // String
inputs['value'] = value; // String
inputs['parent_incident_gr'] = parentIncidentGr; // GlideRecord of table: incident

gs.log('update child incident \n\n' + JSON.stringify(inputs));

sn_fd.FlowAPI.getRunner().action('global.update_child_incidents').inBackground().withInputs(inputs).run();

That should give you an indication if values given to the flow action are correct.

 

PS.

just to check, do you have any business rule(s) blocking updates on a child incidents? That would also block this.

 

@Jorn van Beek 

Thank you for the help Jorn, the function is working but it is updating only the latest opened child incident and not the remaining ones.

Also, for the log statement '

gs.log('update child incident \n\n' + JSON.stringify(inputs));

The inputs are 

{"field":"work_notes","value":"Work note copied from Parent Incident: test update 5","parent_incident_gr":{"sys_meta":{"active":"1","array":"0","attributes":"all_tables.query_hints=true,email_client=true,hasLabels=true,hasWorkflow=true,live_feed=true,query_hints=true","audit":"1","calculation":"","choice":"1","choice_field":"","choice_table":"","create_roles":"","default_value":"","delete_roles":"itil_admin","dependent":"","dependent_on_field":"","display":"number","dynamic_creation":"0","dynamic_creation_script":"","dynamic_default_value":"","dynamic_ref_qual":"","element_reference":"0","filterable":"1","foreign_database":"","function_definition":"","function_field":"0","groupable":"1","help":"","hint":"","i18n_sortable":"1","internal_type":"collection","label":"Incident","language":"en","mandatory":"0","matchable":"1","max_length":"40","multi_text":"0","name":"incident","plural":"Incidents","primary":"0","read_only":"0","read_roles":"","reference":"","reference_cascade_rule":"","reference_floats":"0","reference_key":"","reference_qual":"","reference_qual_condition":"","reference_type":"","sizeclass":"130178","sortable":"1","spell_check":"0","staged":"0","sys_package":"f9e0b8c4db4113007db766a25b961993","sys_scope":"global","table_reference":"0","text_index":"1","type":"0","type_description":"collection","unique":"0","url":"","url_target":"","use_dynamic_default":"0","use_reference_qualifier":"simple","virtual":"0","widget":"","write_roles":"","xml_view":"0"},"promoted_by":{},"parent":{},"caused_by":{},"watch_list":{},"u_incoming_attachments":{},"upon_reject":{},"sys_updated_on":{},"approval_history":{},"u_requester":{},"proposed_by":{},"number":{},"lessons_learned":{},"state":{},"sys_created_by":{},"knowledge":{},"order":{},"u_problem_resolve_resolution_state":{},"u_updater":{},"cmdb_ci":{},"delivery_plan":{},"u_knowledge_creator":{},"impact":{},"active":{},"work_notes_list":{},"priority":{},"sys_domain_path":{},"u_complaint_ticket":{},"rejection_goto":{},"business_duration":{},"group_list":{},"u_incident_age":{},"approval_set":{},"wf_activity":{},"major_incident_state":{},"universal_request":{},"short_description":{},"correlation_display":{},"work_start":{},"delivery_task":{},"trigger_rule":{},"additional_assignee_list":{},"u_gxp_regulated":{},"notify":{},"sys_class_name":{},"service_offering":{},"closed_by":{},"follow_up":{},"parent_incident":{},"u_external_ticketid":{},"reopened_by":{},"reassignment_count":{},"u_external_group":{},"assigned_to":{},"variables":{},"variable_pool":{},"hierarchical_variables":{},"u_ext_symptom":{},"u_outage_end":{},"sla_due":{},"comments_and_work_notes":{},"u_escalation":{},"escalation":{},"upon_approval":{},"timeline":{},"correlation_id":{},"u_business_area":{},"u_associated_ticket":{},"u_external_wf_task":{},"u_all_tasks_closed":{},"made_sla":{},"u_external_ci":{},"u_copy_all_html_attchments":{},"promoted_on":{},"u_mi_confirmed_by_user":{},"child_incidents":{},"hold_reason":{},"task_effective_number":{},"u_major_incident_technical_bridge":{},"resolved_by":{},"u_incident_task":{},"sys_updated_by":{},"u_external_impact":{},"u_external_interface_retry":{},"opened_by":{},"user_input":{},"sys_created_on":{},"u_attachment_file_name":{},"sys_domain":{},"proposed_on":{},"actions_taken":{},"route_reason":{},"u_refer_to_kb":{},"calendar_stc":{},"closed_at":{},"u_external_category":{},"business_service":{},"business_impact":{},"rfc":{},"time_worked":{},"expected_start":{},"opened_at":{},"work_end":{},"reopened_time":{},"resolved_at":{},"caller_id":{},"u_child_count":{},"subcategory":{},"work_notes":{},"close_code":{},"assignment_group":{},"business_stc":{},"cause":{},"description":{},"calendar_duration":{},"u_outage_start":{},"close_notes":{},"sys_id":{},"contact_type":{},"u_external_urgency":{},"incident_state":{},"u_external_interface":{},"urgency":{},"u_category":{},"u_external_state":{},"problem_id":{},"u_ext_close_reason":{},"company":{},"activity_due":{},"severity":{},"overview":{},"comments":{},"approval":{},"due_date":{},"sys_mod_count":{},"reopen_count":{},"sys_tags":{},"u_external_change_type":{},"u_interface_response":{},"u_rca_date_time":{},"u_ext_priority":{},"u_drag_drop_html":{},"location":{},"category":{}}}

Could you please help me with this. 

 

Thank you