Close Case Script - Add Work Notes

Jason116
Tera Contributor

I am writing a background script to close out a large number of cases that have been stuck in the Resolved state. The script itself is fairly straightforward — it simply closes the cases and updates a few fields such as Account, Contact, Assigned To, and Category.

However, I’ve encountered an issue: I need to silence notifications as part of the process, but I also need to add a work note to each case. It seems that by suppressing notifications, journal field updates (like work notes) are also being suppressed, and as a result, the work notes are not being added. Everything else in the script is functioning correctly — it’s just the work notes that aren’t showing up

 

var caseNumbers = [
    'CS0xxxxx',
    'CS0xxxxx',
    'CS01xxxx'
];

// Set field values
var accountSysId = '';
var contactSysId = '';
var assignedToSysId = '6';
var categoryValue = '39';

var workNoteText =
"Add work note here........)";

for (var i = 0; i < caseNumbers.length; i++) {
    var caseNumber = caseNumbers[i];

    var gr = new GlideRecord('sn_customerservice_case');
    gr.addQuery('number', caseNumber);
    gr.query();

    if (gr.next()) {
        gr.setWorkflow(false);
        gr.autoSysFields(false);

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);

        gr.update();

        gr.addWorkNote(workNoteText);
        gr.update();

        gs.print('Case ' + caseNumber + ' updated successfully.');
    } else {
        gs.print('Case ' + caseNumber + ' not found.');

 

7 REPLIES 7

Robert H
Mega Sage

Hello @Jason116 ,

 

Please try the following:

 

...
    if (gr.next()) {
        gr.setWorkflow(false);
        gr.autoSysFields(false);

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);
        gr.update();

        gr.autoSysFields(true); // required for work notes to get added
        gr.work_notes = workNoteText; // there is no "addWorkNotes" method
        gr.update();

        gs.print('Case ' + caseNumber + ' updated successfully.');
    }
...

 

The only notification that could be triggered would the one for work notes being added (if you have such a notification). This is unavoidable.

 

Regards,

Robert

Roshnee Dash
Kilo Sage

To ensure that work notes are added properly while still suppressing notifications, you should:

  1. Update the record with field changes first (with notifications suppressed).
  2. Then re-enable workflow/system fields before adding the work note.

Here’s how you can modify your script:

var caseNumbers = [
    'CS0xxxxx',
    'CS0xxxxx',
    'CS01xxxx'
];

var accountSysId = '';
var contactSysId = '';
var assignedToSysId = '6';
var categoryValue = '39';

var workNoteText = "Add work note here........";

for (var i = 0; i < caseNumbers.length; i++) {
    var caseNumber = caseNumbers[i];

    var gr = new GlideRecord('sn_customerservice_case');
    gr.addQuery('number', caseNumber);
    gr.query();

    if (gr.next()) {
        // Suppress notifications and auto fields for main update
        gr.setWorkflow(false);
        gr.autoSysFields(false);

        gr.setValue('state', '3');
        gr.setValue('account', accountSysId);
        gr.setValue('contact', contactSysId);
        gr.setValue('assigned_to', assignedToSysId);
        gr.setValue('category', categoryValue);
        gr.update();

        // Re-fetch the record to ensure clean state
        var grNote = new GlideRecord('sn_customerservice_case');
        if (grNote.get(gr.sys_id)) {
            // Enable workflow for journal field update
            grNote.setWorkflow(true);
            grNote.autoSysFields(true);
            grNote.work_notes = workNoteText;
            grNote.update();
        }

        gs.print('Case ' + caseNumber + ' updated successfully.');
    } else {
        gs.print('Case ' + caseNumber + ' not found.');
    }
}

instead of providing the number inside array you can directly glide the record by using stateIsresolved something like that.

Hi @Jason116 

If you found my response helpful, please mark it as correct and close the thread so others can benefit from it too.