Help Needed to Create a Business Rule for Updating Documentation After Case Closure

ronro2
Tera Contributor

Hi guys!

I have a problem that concerns a custom table where templates have been created for users to configure general information when new records are made from these templates (table: x_vgll_decom_decommission_template).

On this template there is a field called 'log_close_notes_to', with three different values: 'system_info', 'analysis_existing' & 'information_analysis'. 

In each x_vgll_decom_decommission_task record created under parent x_vgll_decom_decommission, there is a common 'close_notes' field. This close_notes field then writes data to the parent, in either the 'gathered_information', 'performed_analyses' or 'results_information_analyses' field on the parent (x_vgll_decom_decommission).

- So template "Investigate Existing Usage" (x_vgll_decom_decommission_template) with sys_id 30175f5c10520ad8ebb66a18cdd35b2f, 'analysis_existing' chosen,

should be mapped to field 'performed_analyses' on x_vgll_decom_decommission.

- Template "Investigate conditions" (x_vgll_decom_decommission_template) (connected has sys_id 12473e1c109ac6d8ebb66a18cdd35b2f, 'system_info' chosen, should be mapped to 'gathered_information' on x_vgll_decom_decommission.

- Template "Perform Information Analysis" (x_vgll_decom_decommission_template), has sys_id 5937179810d20ad8ebb66a18cdd35bff, 

information_analysis chosen, should be mapped to 'results_information_analyses' on x_vgll_decom_decommission.

 

Remember that close_notes is a common field for all x_vgll_decom_decommission_task records and this is linked to the three different ones on the parent x_vgll_decom_decommission.

 

Here is a script that comes from a subflow activity that you can take inspiration from:

(function execute(inputs, outputs) {
var field = "";
if (inputs.log_section == 'system_info') {
field = 'gathered_information';
}
if (inputs.log_section == 'analysis_existing') {
field = 'performed_analyses';
}
if (inputs.log_section == 'information_analysis') {
field = 'results_information_analyses';
}

if (field != "") {
var decom = new GlideRecord('x_vgll_decom_decommission');
decom.get(inputs.decommission);
if (decom.isValidRecord()) {
var gdt = new GlideDateTime();
if (inputs.close_notes) {
var currVal = decom.getValue(field);
decom.setValue(field, '[' + gdt.getDisplayValue() + '] ' + inputs.short_description + ' : ' + (currVal ? currVal : '') + inputs.close_notes + '\n\n')
decom.update();
}
}
}

})(inputs, outputs);

 

Need to use this in the form of a Business Rule instead. The idea with the Business Rule is to be able to make changes to the documentation after closing a case. Currently, it is not possible after closing the case, as the flow is run once.

 

How would you create this business rule? My Business Rule does not work, this is how it looks like: 

(function executeRule(current, previous /*null when async*/) {
    var field = "";
    switch (current.log_close_notes_to) {
        case 'system_info':
            field = 'gathered_information';
            break;
        case 'analysis_existing':
            field = 'performed_analyses';
            break;
        case 'information_analysis':
            field = 'results_information_analyses';
            break;
    }

    if (field != "") {
        var decom = new GlideRecord('x_vgll_decom_decommission');
        decom.get(current.parent.sys_id);
        if (decom.isValidRecord()) {
            var gdt = new GlideDateTime();
            if (current.close_notes) {
                var currVal = decom.getValue(field);
                decom.setValue(field, '[' + gdt.getDisplayValue() + '] ' + current.short_description + ' : ' + (currVal ? currVal : '') + current.close_notes + '\n\n');
                decom.update();
            }
        }
    }
})(current, previous);

 

ronro2_0-1744728147121.png


And here is what I want to do, so when I write anything in this 'close_notes' field and save the task...

ronro2_0-1744728288511.png


...this field should be updated with information below first timestamp: 

ronro2_1-1744728355024.png

 

 Same goes for the two other task types. 

Thanks in advance!


1 ACCEPTED SOLUTION

That makes perfect sense now, thanks for pointing that out.
So instead of relying on log_close_notes_to directly on the task, we actually need to retrieve it from the referenced decommission_template.
The corrected approach: we will load the decommission_template record (from the task’s reference field), and then use its log_close_notes_to value to determine which field on the parent to update.

Updated Business Rule Code:

(function executeRule(current, previous) {
// Exit if there's no change to close_notes or no parent
if (!current.close_notes.changes() || !current.parent || !current.decommission_template) return;

// Get the template to determine where to log
var template = new GlideRecord('x_vgll_decom_decommission_template');
if (!template.get(current.decommission_template)) return;

var field = "";
switch (template.getValue('log_close_notes_to')) {
case 'system_info':
field = 'gathered_information';
break;
case 'analysis_existing':
field = 'performed_analyses';
break;
case 'information_analysis':
field = 'results_information_analyses';
break;
}

if (field !== "") {
var decom = new GlideRecord('x_vgll_decom_decommission');
if (decom.get(current.parent)) {
var gdt = new GlideDateTime();
var timestamp = '[' + gdt.getDisplayValue() + '] ';
var noteText = timestamp + current.short_description + ' : ' + current.close_notes + '\n\n';

var existing = decom.getValue(field) || "";
decom.setValue(field, existing + noteText);
decom.update();

// Optional: log it for debugging
gs.info(`[Decom Task BR] Appended close_notes to '${field}' on parent record ${current.parent}`);
}
}
})(current, previous);

 

View solution in original post

3 REPLIES 3

SasiChanthati
Giga Guru

Please try this, in the Business Rule:

  • Triggers on update of the x_vgll_decom_decommission_task table

  • Checks if the close_notes field has changed

  • Identifies the parent x_vgll_decom_decommission record

  • Determines which field (gathered_information, performed_analyses, results_information_analyses) to update

  • Appends the close notes with a timestamp and description

General Settings

  • Table: x_vgll_decom_decommission_task

  • When: after

  • Update: Yes

  • Insert: No

  • Delete: No

  • Advanced: Yes

Condition: Add this to ensure the rule runs only if close_notes has changed:

current.close_notes.changes() && current.close_notes

 

Script:

 

(function executeRule(current, previous) {
// Only continue if close_notes has changed and parent exists
if (!current.close_notes || !current.parent) return;

var field = "";
switch (current.log_close_notes_to) {
case 'system_info':
field = 'gathered_information';
break;
case 'analysis_existing':
field = 'performed_analyses';
break;
case 'information_analysis':
field = 'results_information_analyses';
break;
}

if (field !== "") {
var decom = new GlideRecord('x_vgll_decom_decommission');
if (decom.get(current.parent)) {
var gdt = new GlideDateTime();
var timestamp = '[' + gdt.getDisplayValue() + '] ';
var noteText = timestamp + current.short_description + ' : ' + current.close_notes + '\n\n';

var existing = decom.getValue(field) || "";
decom.setValue(field, existing + noteText);
decom.update();
}
}
})(current, previous);

 

Issue Fix / Note

current.parent.sys_idUse current.parent directly — it auto-resolves the sys_id
close_notes.changes()Makes sure the logic doesn’t run every time unless the field changed
setValue() formattingAdds a timestamp and short description above each entry
after ruleImportant so the data is saved and available for update

 

  • Prevent duplicate entries if the same note is edited repeatedly

  • Add current.state.changesTo("Closed") if you only want this to run when the task is closed

 

 

  • Add a gs.info() log line for audit/debug

  • Trigger an email notification to the task owner or approver

 

 

@SasiChanthati It didn't really work. The script somehow does not know when and what template is used in a task. There is a reference field on the 'x_vgll_decom_decommission_task' called 'decommission_template', which refers to 'x_vgll_decom_decommission_template' table, that I forgot to mention. 

That makes perfect sense now, thanks for pointing that out.
So instead of relying on log_close_notes_to directly on the task, we actually need to retrieve it from the referenced decommission_template.
The corrected approach: we will load the decommission_template record (from the task’s reference field), and then use its log_close_notes_to value to determine which field on the parent to update.

Updated Business Rule Code:

(function executeRule(current, previous) {
// Exit if there's no change to close_notes or no parent
if (!current.close_notes.changes() || !current.parent || !current.decommission_template) return;

// Get the template to determine where to log
var template = new GlideRecord('x_vgll_decom_decommission_template');
if (!template.get(current.decommission_template)) return;

var field = "";
switch (template.getValue('log_close_notes_to')) {
case 'system_info':
field = 'gathered_information';
break;
case 'analysis_existing':
field = 'performed_analyses';
break;
case 'information_analysis':
field = 'results_information_analyses';
break;
}

if (field !== "") {
var decom = new GlideRecord('x_vgll_decom_decommission');
if (decom.get(current.parent)) {
var gdt = new GlideDateTime();
var timestamp = '[' + gdt.getDisplayValue() + '] ';
var noteText = timestamp + current.short_description + ' : ' + current.close_notes + '\n\n';

var existing = decom.getValue(field) || "";
decom.setValue(field, existing + noteText);
decom.update();

// Optional: log it for debugging
gs.info(`[Decom Task BR] Appended close_notes to '${field}' on parent record ${current.parent}`);
}
}
})(current, previous);